Difference between highest and least frequencies in an array | C # code to find the difference between highest and least frequencies ; Put all elements in a hash map ; Find counts of maximum and minimum frequent elements ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int findDiff ( int [ ] arr , int n ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . ContainsKey ( arr [ i ] ) ) { var val = mp [ arr [ i ] ] ; mp . Remove ( arr [ i ] ) ; mp . Add ( arr [ i ] , val + 1 ) ; } else { mp . Add ( arr [ i ] , 1 ) ; } } int max_count = 0 , min_count = n ; foreach ( KeyValuePair < int , int > entry in mp ) { max_count = Math . Max ( max_count , entry . Value ) ; min_count = Math . Min ( min_count , entry . Value ) ; } return ( max_count - min_count ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 7 , 8 , 4 , 5 , 4 , 1 , 1 , 7 , 7 , 2 , 5 } ; int n = arr . Length ; Console . WriteLine ( findDiff ( arr , n ) ) ; } }