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
function removeDuplicates ( arr , n ) { let st = 0 ; for ( let 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 ++ ; } } document . write ( " " ) ; for ( let i = 0 ; i < st ; i ++ ) { document . write ( arr [ i ] ) ; if ( i != st - 1 ) document . write ( " " ) ; } document . write ( " " ) ; } let arr = [ 1 , 1 , 1 , 2 , 2 , 2 , 3 , 3 , 3 , 3 , 3 , 3 , 4 , 5 ] ; let n = arr . length ; removeDuplicates ( arr , n ) ;