Traverse the map and find minimum element with frequency k .
int res = int . MaxValue ; HashSet < int > s = new HashSet < int > ( m . Keys . ToArray ( ) ) ; foreach ( int temp in s ) if ( m [ temp ] == k ) res = Math . Min ( res , temp ) ; return ( res != int . MaxValue ) ? res : - 1 ; }