Last active
August 29, 2015 14:14
-
-
Save petomalina/1ffcfd4cc0b35beb04ef to your computer and use it in GitHub Desktop.
Find duplicates in the given array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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