Remove duplicate elements from sorted Array | Practice | GeeksforGeeks

Given a sorted array A[] of size N, delete all the duplicates elements from A[].
Note: Don't use set or HashMap to solve the problem.

Example 1:
Input:
N = 5
Array = {2, 2, 2, 2, 2}
Output: 2
Explanation: After removing all the duplicates 
only one instance of 2 will remain.

Example 2:
Input:
N = 3
Array = {1, 2, 2}
Output: 1 2 

Your Task:  
You don't need to read input or print anything. Complete the function remove_duplicate() which takes the array A[] and its size N as input parameters and modifies it in place to delete all the duplicates. The function must return an integer X denoting the new modified size of the array. 
Note: The generated output will print all the elements of the modified array from index 0 to X-1.

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)

Constraints:
1 ≤ N ≤ 104
1 ≤ A[i] ≤ 106
0
aravindmn75832 days ago
//c++ solution;
class Solution{public:   int remove_duplicate(int a[],int n){       // code here       int res = 0;              for(int i=0; i<n; i++){           if(a[i]!=a[i+1]){               a[res] = a[i];               res++;           }       }       return res;   }};
0
sahum67033 days ago
easiest c++ solution || rumtime 0.48/1.38
 
int remove_duplicate(int arr[],int n){
        int ans=n;
        int k=arr[0];
        for(int i=1;i<n;i++){
            if(arr[i]==k){
                arr[i]=INT_MAX;
                ans--;
            }
            else
                k=arr[i];
        }
        sort(arr,arr+n);
        return ans;
    }
0
animesh10311 week ago
int remove_duplicate(int a[],int n){       // code here        int K = 1;       int current = a[0];       for(int i = 1; i < n; i++)        {          if(current != a[i])           {              a[K] = a[i];              K++;              current = a[i];          }           else           {              a[i] = 999;          }       }       return K;   }
0
shyampatel21 week ago
Python ! Not O(n) solution but it passed all tests lol def remove_duplicate(self, A, N): # code here A[:] = sorted(set(A)) return len(A)
0
vinayakmagd96alt2 weeks ago
JAVA | Easy solution | Time complexity O(n)
 
int remove_duplicate(int A[],int N){
        int index = 1;
        int prev = A[0];
        for(int i = 1; i < N; i++) {
           if(prev != A[i]) {
               A[index] = A[i];
               index++;
               prev = A[i];
           } else {
               A[i] = 0;
           }
        }
        return index;
    }
0
achaudhary1732 weeks ago
//User function Template for Java
class Solution {   int remove_duplicate(int A[],int N){              if(N == 0){           return 0;       }              int j = 1;       for(int i =0; i < A.length - 1; i++ ){           if (A[i] != A[i + 1]){                              A[j] = A[i + 1];               j++;                                         }       }       return j;   }}
0
vaishalisonvane282 weeks ago
class Solution {   int remove_duplicate(int A[],int N){       Arrays.sort(A);       int j=0;       for(int i=0; i<N-1; i++){                  if(A[i]!=A[i+1]){                         A[j++] = A[i];           }       }       A[j++] = A[N-1];       return j;   }  }     
+1
harshitjinodiya4 weeks ago
Java Solution
 
class Solution {   int remove_duplicate(int A[],int N){       int res=1;       for(int i=1;i<N;i++)       {           if(A[i]!=A[res-1])           {               A[res] = A[i];               res++;           }       }       return res;   }}
+3
swapniltayal4221 month ago
class Solution{public:   int remove_duplicate(int a[],int n){       // code here       int k = 1;       for (int i=1; i<n; i++){           if (a[i] != a[k-1]){               a[k] = a[i];               k++;           }       }return k;   }};
0
sheikhsa21 month ago
 def remove_duplicate(self, A, N): # code here i = 0 while len(A) - 1 > i:     if A[i+1] == A[i]:         A.pop(i+1)     else:         i += 1 return len(A)     
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.