C Program to Print all the Repeated Numbers with Frequency in Array

Write a C Program to Print all the Repeated Numbers with Frequency in Array. So first approach is to use linear search and find the occurrence of numbers but efficient solution is to use less time and space complexity.

For Example if you given array A[ ] = { 5, 3, 2, 5, 3, 1}; to find frequency of numbers, So here is small solution with  Time and Space complexity with O(n) .

 

Here array A is our main array in which we want to find frequency of numbers and array OccurCount is used to count element occurrences.

Logic

The logic of this problem is pretty simple

  1. We declare new array OccurCount of same size as A.
  2. Increment the value in OccurCount array at index same as element. For example if we get first element ‘5’ in array A , then increment value of element in array OccurCount at index 5.

C Program to Print all the Repeated Numbers with Frequency in Array

 

Output

C Program to Print all the Repeated Numbers with Frequency in Array