Reduce the array such that each element appears at most 2 times
Given a sorted array arr of size N, the task is to reduce the array such that each element can appear at most two times.
Examples:
Input: arr[] = {1, 2, 2, 2, 3}
Output: {1, 2, 2, 3}
Explanation:
Remove 2 once, as it occurs more than 2 times.Input: arr[] = {3, 3, 3}
Output: {3, 3}
Explanation:
Remove 3 once, as it occurs more than 2 times.
Approach: This can be solved with the help of two pointer algorithm.
- Start travsering array from left and maintain two pointers.
- One pointer (lets say i) is used to iterate the array.
- And the second pointer (lets say st) moves forward to find the next unique element, the element at i appears more than twice.
Below is the implementation of the above approach:
CPP
// C++ program to reduce the array// such that each element appears// at most 2 times#include <bits/stdc++.h>using namespace std;// Function to remove duplicatesvoid removeDuplicates(int arr[], int n){ // Initalise 2nd pointer int st = 0; // Itereate over the array for (int i = 0; i < n; i++) { if (i < n - 2 && arr[i] == arr[i + 1] && arr[i] == arr[i + 2]) continue; // Updating the 2nd pointer else { arr[st] = arr[i]; st++; } } cout << "{"; for (int i = 0; i < st; i++) { cout << arr[i]; if (i != st - 1) cout << ", "; } cout << "}";}// Driver codeint main(){ int arr[] = { 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 5 }; int n = sizeof(arr) / sizeof(arr[0]); // Function call removeDuplicates(arr, n); return 0;} |
Java
// Java program to reduce the array// such that each element appears// at most 2 timesclass GFG{// Function to remove duplicatesstatic void removeDuplicates(int arr[], int n){ // Initalise 2nd pointer int st = 0; // Itereate over the array for (int i = 0; i < n; i++) { if (i < n - 2 && arr[i] == arr[i + 1] && arr[i] == arr[i + 2]) continue; // Updating the 2nd pointer 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("}");}// Driver codepublic 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; // Function call removeDuplicates(arr, n);}}// This code is contributed by sapnasingh4991 |
Python3
# Python3 program to reduce the array# such that each element appears# at most 2 times# Function to remove duplicatesdef removeDuplicates(arr, n) : # Initalise 2nd pointer st = 0; # Itereate over the array for i in range(n) : if (i < n - 2 and arr[i] == arr[i + 1] and arr[i] == arr[i + 2]) : continue; # Updating the 2nd pointer else : arr[st] = arr[i]; st += 1; print("{",end="") for i in range(st) : print(arr[i],end=""); if (i != st - 1) : print(", ",end=""); print("}",end="");# Driver codeif __name__ == "__main__" : arr = [ 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 5 ]; n = len(arr); # Function call removeDuplicates(arr, n);# This code is contributed by Yash_R |
C#
// C# program to reduce the array// such that each element appears// at most 2 timesusing System;class GFG{ // Function to remove duplicatesstatic void removeDuplicates(int []arr, int n){ // Initalise 2nd pointer int st = 0; // Itereate over the array for (int i = 0; i < n; i++) { if (i < n - 2 && arr[i] == arr[i + 1] && arr[i] == arr[i + 2]) continue; // Updating the 2nd pointer else { arr[st] = arr[i]; st++; } } Console.Write("{"); for (int i = 0; i < st; i++) { Console.Write(arr[i]); if (i != st - 1) Console.Write(", "); } Console.Write("}");} // Driver codepublic 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; // Function call removeDuplicates(arr, n);}}// This code is contributed by sapnasingh4991 |
Output
{1, 1, 2, 2, 3, 3, 4, 5}Time complexity: O(N)
Space complexity: O(1)
Another Approach : Using Counter() function
- Calculate the frequency of all elements using a counter function.
- Take an empty list.
- Traverse the array.
- If the frequency of any element is greater than or equal to 2 make its frequency to 1 and append it to the list.
- If the frequency of any element is equal to 1 make its frequency to 0 and append it to the list.
- Print the list.
Below is the implementation of the above approach:
Python3
# Python3 program to reduce the array# such that each element appears# at most 2 timesfrom collections import Counter# Function to remove duplicatesdef removeDuplicates(arr, n): freq = Counter(arr) # Taking empty list l = [] for i in range(n): if(freq[arr[i]] >= 2): # Making frequency to 1 freq[arr[i]] = 1 l.append(arr[i]) elif(freq[arr[i]] == 1): # Making frequency to 0 # and appending to list l.append(arr[i]) freq[arr[i]] = 0 # Printing the list for i in l: print(i, end=" ")# Driver codeif __name__ == "__main__": arr = [1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 5] n = len(arr) # Function call removeDuplicates(arr, n)# This code is contributed by vikkycirus |
Output
1 1 2 2 3 3 4 5
Time complexity: O(N)
Space Complexity: O(N)


