+ 1

How to find and print duplicate numbers of an array in C++?

For example, if the array has 1,2,1,3,2 then the program will print print 1 2

24th Nov 2017, 12:01 PM
Md. Asiful Hoque Prodhan
Md. Asiful Hoque Prodhan - avatar
1 Odpowiedź
+ 1
If you want the duplicates printed in the order they appear in the original array use a vector. Loop through the array, if the element is a duplicate add it to the vector then print the new vector. https://code.sololearn.com/cJJ835tu5vSS/#cpp if you're fine with the elements appearing in sorted order, which in your case is the same as the the order they appear in the array, then you can use a set. Set's can only contain unique values, that is they cannot contain duplicates, because of this we don't have to check to see if the element has already been added as we do with the vector version. Sets are faster than vectors because we don't have to step through the whole collection to see if an element has already been added. https://code.sololearn.com/cDKtuqOalDA1/#cpp Which version should you use? Use the set version when you don't mind the output being sorted in ascending order, use the vector version if you want the output to appear in the same order it appeared in the original array. Hope this helps!
24th Nov 2017, 6:19 PM
Phill