Difference of count of distinct elements present to left and right for each array element | Function to find the difference of count of distince elements to the left and right for each array elements ; Stores distinct array element in the left and right ; Traverse the array ; Insert all element to the left in the set S1 ; Insert all element to the right in the set S2 ; Print the difference ; Driver Code
function findDifferenceArray ( arr , N ) { let S1 = new Set ( ) ; let S2 = new Set ( ) ; for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < i ; j ++ ) { S1 . add ( arr [ j ] ) ; } for ( let j = i + 1 ; j < N ; j ++ ) { S2 . add ( arr [ j ] ) ; } document . write ( Math . abs ( S1 . size - S2 . size ) + ' ' ) ; S1 . clear ( ) ; S2 . clear ( ) ; } } let arr = [ 7 , 7 , 3 , 2 , 3 ] ; let N = arr . length ; findDifferenceArray ( arr , N ) ;