+ 1

Write a program to find out that how many numbers has how many duplicates ?

Input : arr [10]={1,2,3,3,2,1,6,6,2,2} output : 1 is repeated 2 times 2 is repeated 4 times 3 is repeated 2 times 6 is repeated 2 times

23rd Jan 2017, 8:17 PM
Arshdeep singh
Arshdeep singh - avatar
1 Odpowiedź
+ 3
Probably easiest if you sort the array first (I used quick sort) and after that count the numbers. void qsort(int input[], int left, int right){ if(right <= left+1){ return; } int t = input[right]; int w = left; for(int i = left; i < right; i++){ if(input[i] < t){ input[w] = input[w] + input[i] - (input[i] = input[w]); w++; } } input[w] = input[w] + input[right] - (input[right] = input[w]); qsort(input, left, w-1); qsort(input, w+1, right); } void count_repeats(int input[], int n){ int t = 1; for(int i = 1; i < n; i++){ if(input[i] == input[i-1]){ t++; } else { cout << input[i-1] << " is repeated " << t << " times" << endl; t = 1; } } cout << input[n-1] << " is repeated " << t << " times" << endl; }
23rd Jan 2017, 9:13 PM
Robobrine
Robobrine - avatar