-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemove_Duplicates.cpp
71 lines (58 loc) · 1.51 KB
/
Remove_Duplicates.cpp
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* PROBLEM STATEMENT--->
Given an array of sorted numbers, remove all duplicates from it. You should not use any extra space; after removing the duplicates in-place return the new length of the array.*/
//EXAMPLE----->
/*Input: [2, 3, 3, 3, 6, 9, 9]
Output: 4
Explanation: The first four elements after removing the duplicates will be [2, 3, 6, 9]. */
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
class Remove_Duplicates
{
public:
static int Find_Duplicates_Remove(const vector<int> arr)
{
int start_pointer=0;
int next_pointer=1;
int length=arr.size();
if(length==0)
{
return 0;
}
while(start_pointer<next_pointer && next_pointer<arr.size())
{
if(arr[start_pointer]==arr[next_pointer])
{
length-=1;
next_pointer+=1;
}
else
{
start_pointer=next_pointer;
next_pointer+=1;
}
}
return length;
}
};
int main()
{
int result=Remove_Duplicates::Find_Duplicates_Remove( vector<int> {});
cout<<result;
}
//Another Approach to this problem goes like---->
/*
static int remove(vector<int> &arr)
{
int nextNonDuplicate=1;
for(int i=1;i<arr.size();i++)
{
if(arr[nextNonDuplicate-1]!=arr[i])
{
arr[nextNonDuplicate]=arr[i];
nextNonDuplicate++;
}
}
return nextNonDuplicate;
}
*/