Let\'s say I have an array of ints {100, 80, 90, 100, 80, 60}
so I want to count those duplicates and save those counter for later. because each duplicate number sh
Approach 1
The easiest way, is not to sort your array, and increment elements of a map:
unordered_map count; // holds count of each encountered number
for (int i=0; i
You can then process the content of the map:
for (auto &e:count) // display the result
cout << e.first <<" : "< "<
If needed, filter out the non duplicates by rerasing them from the map or ignoring it during the processing.
Approach 2
If you're not allowed to use maps, then you have to elaborate your counting loop, in order to restart counting for each new number, and being able to process consecutive dups also if more than two:
...
for(int i = 0; i < number; i+=counter) {
for (counter=1; i+counter1) { // if more than one, process the dups.
cout << "dup: " << array[i] << " "<
If you need to store the pairs to process them in asecond step, you need to store a pair (preferably in a vector, but if needed in an array):
pair result[number]; // a vector would be preferable
int nres=0;
...
if (counter>1) { // if more than one, process the dups.
// cout << "dup: " << array[i] << " "<
Online demo for both approaches