Smallest subarray with all occurrences of a most frequent element | JavaScript implementation to find smallest subarray with all occurrences of a most frequent element ; To store left most occurrence of elements ; To store counts of elements ; To store maximum frequency ; To store length and starting index of smallest result window ; First occurrence of an element , store the index ; increase the frequency of elements ; Find maximum repeated element and store its last occurrence and first occurrence ; length of subsegment ; select subsegment of smallest size ; Print the subsegment with all occurrences of a most frequent element ; Driver program
function smallestSubsegment ( a , n ) { let left = new Map ( ) ; let count = new Map ( ) ; let mx = 0 ; let mn = - 1 , strindex = - 1 ; for ( let i = 0 ; i < n ; i ++ ) { let x = a [ i ] ; if ( count . get ( x ) == null ) { left . set ( x , i ) ; count . set ( x , 1 ) ; } else count . set ( x , count . get ( x ) + 1 ) ; if ( count . get ( x ) > mx ) { mx = count . get ( x ) ; mn = i - left . get ( x ) + 1 ; strindex = left . get ( x ) ; } else if ( ( count . get ( x ) == mx ) && ( i - left . get ( x ) + 1 < mn ) ) { mn = i - left . get ( x ) + 1 ; strindex = left . get ( x ) ; } } for ( let i = strindex ; i < strindex + mn ; i ++ ) document . write ( a [ i ] + " " ) ; } let A = [ 1 , 2 , 2 , 2 , 1 ] ; let n = A . length ; smallestSubsegment ( A , n ) ;