Insert all elements in hash
Dictionary < int , int > hp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int key = arr [ i ] ; if ( hp . ContainsKey ( key ) ) { int freq = hp [ key ] ; freq ++ ; hp [ key ] = freq ; } else hp . Add ( key , 1 ) ; }