Store frequencies
Dictionary < int , int > m = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! m . ContainsKey ( A [ i ] ) ) m . Add ( A [ i ] , 1 ) ; else { m [ A [ i ] ] ++ ; } }