arrays - Counting duplicates in C -
if have array set as:
test[10] = {1,2,1,3,4,5,6,7,8,3,2}
i want return number 3 since there duplicate 1, duplicate 2 , duplicate 3. how can achieve this? efficiency doesn't matter.
first must sort array easier find duplicates.
here example of sort (bubble sort):
void bubblesort(int numbers[], int array_size) { int i, j, temp; (i = (array_size - 1); > 0; i--) { (j = 1; j <= i; j++) { if (numbers[j-1] > numbers[j]) { temp = numbers[j-1]; numbers[j-1] = numbers[j]; numbers[j] = temp; } } } }
then loop through again , find if values[i]
==
values[i+1]
note: when create loop make 1 length shorter compensate values[i+1]
not go out of bounds.
Comments
Post a Comment