Skip to content

Instantly share code, notes, and snippets.

@vishalkanaujia
Created August 3, 2019 08:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vishalkanaujia/dfa942355c53b7cb85f741c030ac49cc to your computer and use it in GitHub Desktop.
Save vishalkanaujia/dfa942355c53b7cb85f741c030ac49cc to your computer and use it in GitHub Desktop.
#include<iostream>
#include <vector>
using namespace std;
vector<int> findDuplicates(int arr[], int len) {
vector<int> result;
for(int i = 0; i < len; i++) {
int index = abs(arr[i]) - 1;
if (arr[index] < 0) {
result.push_back(arr[i]);
} else {
arr[index] = -arr[index];
}
}
return result;
}
int main() {
vector<int> v;
int arr[5] = {1,2,2,3,1};
v = findDuplicates(arr, 5);
for (auto i = v.begin(); i != v.end(); i++) {
cout << *i << " ";
}
cout << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment