Difference of count of distinct elements present to left and right for each array element | Java program for the above approach ; 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
import java . util . HashSet ; class GFG { public static void findDifferenceArray ( int arr [ ] , int N ) { HashSet < Integer > S1 = new HashSet < Integer > ( ) ; HashSet < Integer > S2 = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { S1 . add ( arr [ j ] ) ; } for ( int j = i + 1 ; j < N ; j ++ ) { S2 . add ( arr [ j ] ) ; } System . out . print ( Math . abs ( S1 . size ( ) - S2 . size ( ) ) + " ▁ " ) ; S1 . clear ( ) ; S2 . clear ( ) ; } } public static void main ( String args [ ] ) { int arr [ ] = { 7 , 7 , 3 , 2 , 3 } ; int N = arr . length ; findDifferenceArray ( arr , N ) ; } }