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 ; It prints number of occurrences of each element in the array . ; HashMap to store frequencies ; traverse the array ; update the frequency ; traverse the hashmap ; Driver function
using System ; using System . Collections . Generic ; class GFG { static void findFrequency ( int [ ] arr , int n ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! mp . ContainsKey ( arr [ i ] ) ) mp [ arr [ i ] ] = 0 ; mp [ arr [ i ] ] ++ ; } foreach ( KeyValuePair < int , int > kvp in mp ) Console . WriteLine ( " Element ▁ " + kvp . Key + " ▁ occurs ▁ " + kvp . Value + " ▁ times " ) ; } public static void Main ( ) { int [ ] arr = { 1 , 1 , 1 , 2 , 3 , 3 , 5 , 5 , 8 , 8 , 8 , 9 , 9 , 10 } ; int n = arr . Length ; findFrequency ( arr , n ) ; } }