Skip to content

Commit 329c088

Browse files
committed
removeduplicateelement
1 parent 5023427 commit 329c088

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

Programs/Array/removedup.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
/*
3+
4+
WAP in Js Remove duplicate element in present array
5+
6+
7+
What happens here:
8+
new Set(arr)
9+
10+
The Set is a built-in JavaScript object that stores unique values only.
11+
12+
When you pass arr to Set, it automatically removes all duplicate elements.
13+
14+
Example: new Set(arr) → {1, 2, 3, 4, 5, 6, 8} (this is a Set object, not an array)
15+
16+
[... ] — Spread Operator
17+
18+
The spread operator ... is used to spread elements of an iterable (like a Set) into an array.
19+
20+
So [...new Set(arr)] converts the Set back into a regular array.
21+
22+
Final result:
23+
uniqueArr becomes [1, 2, 3, 4, 5, 6, 8]
24+
25+
26+
let arr = [1, 1, 1, 2, 2, 2, 3, 4, 5, 6, 5, 6, 8];
27+
let uniqueArr = [];
28+
29+
for (let i = 0; i < arr.length; i++) {
30+
if (!uniqueArr.includes(arr[i])) {
31+
uniqueArr.push(arr[i]);
32+
}
33+
}
34+
35+
console.log(uniqueArr); // [1, 2, 3, 4, 5, 6, 8]
36+
37+
38+
39+
*/
40+
41+
42+
let arr=[1,1,1,2,2,2,3,4,5,6,5,6,8]
43+
let uniqueArr = [...new Set(arr)];
44+
console.log(uniqueArr); // [1, 2, 3, 4, 5, 6, 8]
45+

0 commit comments

Comments
 (0)