forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Remove_duplicates_from_array.dart
68 lines (57 loc) · 1.23 KB
/
Remove_duplicates_from_array.dart
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
/*
Remove duplicates from an array by using DART.
Given an array, remove duplicate element from it so
that it only consists of unique elements in the array.
*/
import 'dart:io';
void duplicates(var array) {
// sorting the array
array.sort();
for (int i = 0; i < array.length - 1; i++) {
// checking if the next element is same
if (array[i] == array[i + 1]) {
array.remove(array[i + 1]);
i -= 1;
}
}
}
void main() {
var data = [];
// taking input from user and storing it in data array
var n, ele;
print('Enter the number of Elements: ');
n = stdin.readLineSync();
n = int.parse(n);
for (int i = 1; i <= n; i++) {
print('Enter Element $i: ');
ele = stdin.readLineSync();
ele = int.parse(ele);
data.add(ele);
}
print("Entered List is: ");
print(data);
print("\nList after removing duplicates: ");
duplicates(data);
print(data);
}
/*
TIME COMPLEXITY: O(n)
SPACE COMPLEXITY: O(1)
SAMPLE INPUT/OUTPUT:
Enter the number of Elements:
5
Enter Element 1:
1
Enter Element 2:
2
Enter Element 3:
2
Enter Element 4:
1
Enter Element 5:
3
Entered List is:
[1, 2, 2, 1, 3]
List after removing duplicates:
[1, 2, 3]
*/