Reduce the array such that each element appears at most 2 times | Java program to reduce the array such that each element appears at most 2 times ; Function to remove duplicates ; Initialise 2 nd pointer ; Iterate over the array ; Updating the 2 nd pointer ; Driver code ; Function call
class GFG { static void removeDuplicates ( int arr [ ] , int n ) { int st = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i < n - 2 && arr [ i ] == arr [ i + 1 ] && arr [ i ] == arr [ i + 2 ] ) continue ; else { arr [ st ] = arr [ i ] ; st ++ ; } } System . out . print ( " { " ) ; for ( int i = 0 ; i < st ; i ++ ) { System . out . print ( arr [ i ] ) ; if ( i != st - 1 ) System . out . print ( " , ▁ " ) ; } System . out . print ( " } " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 1 , 2 , 2 , 2 , 3 , 3 , 3 , 3 , 3 , 3 , 4 , 5 } ; int n = arr . length ; removeDuplicates ( arr , n ) ; } }