-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ11.cpp
More file actions
39 lines (29 loc) · 789 Bytes
/
Q11.cpp
File metadata and controls
39 lines (29 loc) · 789 Bytes
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
// 11. Remove duplicate elements from the array arr = [1, 2, 2, 3, 4, 4, 5] and print the updated array.
#include <iostream>
#include <algorithm>
using namespace std;
int removeDuplicate(int arr[], int size)
{
if (size <= 1) return size;
sort(arr, arr + size);
int newIndex = 1;
for (int i = 1; i < size; i++) {
if (arr[i] != arr[i - 1]) {
arr[newIndex] = arr[i];
newIndex++;
}
}
return newIndex;
}
int main()
{
int arr[] = {1, 2, 3, 2, 2, 3, 4, 5, 6, 8, 8, 0, 9};
int size = sizeof(arr) / sizeof(arr[0]);
size = removeDuplicate(arr, size);
cout << "Array after removing duplicates: ";
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}