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 duplicates 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++; } } cout << "{"; for (int i = 0; i < st; i++) { cout << arr[i]; if (i != st - 1) cout << ", "; } cout << "}"; } // Driver code int 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 times class GFG { // Function to remove duplicates static 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 code 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; // 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 duplicates def 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 code if __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 times using System; class GFG { // Function to remove duplicates static 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 code 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; // Function call removeDuplicates(arr, n); } } // This code is contributed by sapnasingh4991 |
{1, 1, 2, 2, 3, 3, 4, 5}
Time complexity: O(N)
Space complexity: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Reduce the array such that each element appears at most K times
- Find the element that appears once in an array where every other element appears twice
- First element that appears even number of times in an array
- Find the only element that appears b times
- Lengths of maximized partitions of a string such that each character of the string appears in one substring
- Maximized partitions of a string such that each character of the string appears in one substring
- Count all elements in the array which appears at least K times after their first occurrence
- Maximum number of times str1 appears as a non-overlapping substring in str2
- Maximum difference between two elements such that larger element appears after the smaller number
- Count pairs (p, q) such that p occurs in array at least q times and q occurs at least p times
- Form an array of distinct elements with each element as sum of an element from each array
- Reduce array to longest sorted array possible by removing either half of given array in each operation
- Minimum possible value T such that at most D Partitions of the Array having at most sum T is possible
- Find the element that appears once in a sorted array
- Largest possible Subset from an Array such that no element is K times any other element in the Subset
- Reduce array to a single element by repeatedly removing an element from any increasing pair
- Find the element that appears once
- Smallest number to make Array sum at most K by dividing each element
- Sum of product of each element with each element after it
- Find Array formed by adding each element of given array with largest element in new array to its left
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

