Store frequencies of array elements
Dictionary < int , int > frequency = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( frequency . ContainsKey ( a [ i ] ) ) { var val = frequency [ a [ i ] ] ; frequency . Remove ( a [ i ] ) ; frequency . Add ( a [ i ] , val + 1 ) ; } else { frequency . Add ( a [ i ] , 1 ) ; } } int count = 0 ;