-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpracticep15.cpp
More file actions
42 lines (36 loc) · 1.02 KB
/
practicep15.cpp
File metadata and controls
42 lines (36 loc) · 1.02 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
#include <iostream>
using namespace std;
//sort and remove duplicates
int main() {
int n;
cin >> n; // Read the size of the array
int arr[n];
// Read the elements of the array
for (int i = 0; i < n; ++i) {
cin >> arr[i];
}
// Implementing Bubble Sort to sort the array manually
for (int i = 0; i < n - 1; ++i) {
for (int j = 0; j < n - i - 1; ++j) {
if (arr[j] > arr[j + 1]) {
// Swap elements if they are in the wrong order
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
// Removing duplicates manually
int new_size = 0;
for (int i = 0; i < n; ++i) {
if (i == 0 || arr[i] != arr[new_size - 1]) {
arr[new_size++] = arr[i]; // Only add unique elements
}
}
// Print the sorted array with duplicates removed
for (int i = 0; i < new_size; ++i) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}