From a set of array elements, find
the number of occurrences of
each element present in the given
array.
#include <stdio.h>
#include <stdlib.h>
int main() {
int numbers[]={5,6,5,8,9,6,5,6,2,1,3,9};
int usedNumbers[12];
int i,j;
int counter = 0;
int usedNumbersCounter = 0;
int exists=0;
for (i=0; i<12; i++){
counter = 0;
for (j=0; j<12; j++){
if (numbers[i] == numbers[j]){
counter++;
}
}
exists=0;
for (j=0; j<12; j++){
if (numbers[i] == usedNumbers[j]){
exists=1;
break;
}
}
if(exists==0){
printf("The number %d occurrences %d times\n",numbers[i],counter);
usedNumbers[usedNumbersCounter]=numbers[i];
usedNumbersCounter++;
}
}
getchar();
getchar();
return 0;
}
Comments
Leave a comment