-
Notifications
You must be signed in to change notification settings - Fork 245
Expand file tree
/
Copy pathremove-duplicates-from-sorted-array.cpp
More file actions
54 lines (43 loc) · 1.41 KB
/
remove-duplicates-from-sorted-array.cpp
File metadata and controls
54 lines (43 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//Ques Link: https://www.interviewbit.com/problems/remove-duplicates-from-sorted-array/
/* Remove duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appears only once and return the new length.
Note that even though we want you to return the new length, make sure to change the original array as well in place
Do not allocate extra space for another array, you must do this in place with constant memory.
Example:
Given input array A = [1,1,2],
Your function should return length = 2, and A is now [1,2]. */
//main function has been included to ease readability and for better understanding
#include <bits/stdc++.h>
using namespace std;
int removeDuplicates(vector<int> &A)
{
if (A.size() == 1)
return 1;
int i = 1, j = 1;
while (i < A.size() && j < A.size())
{
if (A[i] != A[j])
{
A[j] = A[i]; //allocate element only if it is unique i.e non duplicate
}
if (A[j - 1] != A[j]) //array is sorted so we compare adjacent indeces
{
j++;
}
i++;
}
return j;
}
int main()
{
int size;
cin >> size;
vector<int> A(size);
for (auto &x : A)
cin >> x;
int newlength = removeDuplicates(A);
cout << "New length after removal of duplicates is " << newlength << endl;
for (int i = 0; i < newlength; i++)
cout << A[i] << " ";
return 0;
}