Check if all subarrays contains at least one unique element | Function to check if all subarrays have at least one unique element ; Generate all subarray ; Store frequency of subarray 's elements  map<int, int> hm; ; Traverse the array over the range [ i , N ] ; Update frequency of current subarray in map hm [ arr [ j ] ] ++ ; ; Increment count ; Decrement count ; If all subarrays have at least 1 unique element ; Given array [ ] arr ; Function Call
function check ( arr , n ) { for ( var i = 0 ; i < n ; i ++ ) { var hm = new Map ( ) ; var count = 0 ; for ( var j = i ; j < n ; j ++ ) { if ( hm . has ( arr [ j ] ) ) hm . set ( arr [ j ] , hm . get ( arr [ j ] ) + 1 ) else hm . set ( arr [ j ] , 1 ) if ( hm . get ( arr [ j ] ) == 1 ) count ++ ; if ( hm . get ( arr [ j ] ) == 2 ) count -- ; if ( count == 0 ) return " " ; } } return " " ; } var arr = [ 1 , 2 , 1 ] ; var N = arr . length ; document . write ( check ( arr , N ) ) ;