 Last Updated:

# C program to check the occurrence of a number in an array ## Algorithm

Input: Input size of the array, elements of the array
output: Displays the occurrence of each number in an array

1. Start
2. Input the size of the array say n and enter the elements of the array a[n]
3. j = 0 , visited = -1
4. while ( j < n )
4.1 count = 1
4.2 k = j + 1
4.3 while ( k < n )
4.3.1 if (a [ j ] = = a [ k ] )
4.3.1.1 count ++
4.3.1.2 b [ k ] = visited
4.3.2 k++
4.4 if b [ j ] ! = visited
4.4.1 b [ j ] = count
4.5 j++
5. j = 0
6. while ( j < n )
6.1 if b [ j ] ! = visited
6.1.1 print "The elements" a[ j ] "appears" b [ j ] "times"
6.2 j++
7. stop

## C Program

Note: This program only works on some compilers. #include <stdio.h>

int main()
{

//Input the size and elements of the array
int n,visited = -1;
printf("Enter the size of the array: ");
scanf("%d", &n);
int a[ n ],b[ n ];
printf("Enter the elements one by one\n");
for(int i=0; i<n; i++)
scanf("%d", &a[ i ]);

//Checking the occurrence
for ( int j = 0; j < n; j++ ) {
int count = 1;
for ( int k = j+1; k < n; k++) {
if ( a[ j ] == a[ k ] ) {
count++;
b[k] = visited;
}
}
if(b[ j ] != visited) {
b[ j ] = count;
} }

// Printing the elements and the number of time each appeared
for ( int j = 0; j < n; j++ ) {
if ( b[ j ] != visited ) {
printf("The element %d appears %d times\n", a[ j ], b[ j ]);
}
}
return 0;
}

Note: This Program was created using visual studio code. The Program file can be downloaded by clicking here

## Output

Enter the size of the array: 5
Enter the elements one by one
1
2
2
3
4
The element 1 appears 1 times
The element 2 appears 2 times
The element 3 appears 1 times
The element 4 appears 1 times