Find frequency of each element in a limited range array in less than O ( n ) time | C # program to count number of occurrences of each element in the array in O ( n ) time and O ( 1 ) space ; check if the current element is equal to previous element . ; reset the frequency ; print the last element and its frequency ; Driver code
using System ; class GFG { public static void findFrequencies ( int [ ] ele ) { int freq = 1 ; int idx = 1 ; int element = ele [ 0 ] ; while ( idx < ele . Length ) { if ( ele [ idx - 1 ] == ele [ idx ] ) { freq ++ ; idx ++ ; } else { Console . WriteLine ( element + " ▁ " + freq ) ; element = ele [ idx ] ; idx ++ ; freq = 1 ; } } Console . WriteLine ( element + " ▁ " + freq ) ; } public static void Main ( String [ ] args ) { Console . WriteLine ( " - - - frequencies ▁ in ▁ a ▁ sorted ▁ array - - - - " ) ; findFrequencies ( new int [ ] { 10 , 20 , 30 , 30 , 30 , 40 , 50 , 50 , 50 , 50 , 70 } ) ; } }