Skip to content

Instantly share code, notes, and snippets.

@petomalina
Last active August 29, 2015 14:14
Show Gist options
  • Save petomalina/1ffcfd4cc0b35beb04ef to your computer and use it in GitHub Desktop.
Save petomalina/1ffcfd4cc0b35beb04ef to your computer and use it in GitHub Desktop.
Find duplicates in the given array
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
template <typename T>
vector<T> *findDuplicates(T *elements, unsigned int length) {
sort(elements, elements + length);
vector<T> *duplicates = new vector<T>();
for(unsigned int i = 1; i < length; i++) {
if(elements[i] == elements[i-1] && find(duplicates->begin(), duplicates->end(), elements[i]) == duplicates->end()) {
duplicates->push_back(elements[i]);
}
}
return duplicates;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment