Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1b4bc08

Browse files
committed
New: Remove duplicate value.
1 parent ca601be commit 1b4bc08

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

‎basic/remove_duplicate_val.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const arrayWithDuplicates = [1, 2, 3, 4, 2, 3, 5, 6, 1];
2+
const uniqueArray = [...new Set(arrayWithDuplicates)];
3+
4+
console.log(uniqueArray); // Output: [1, 2, 3, 4, 5, 6]
5+
6+
// Using filter and indexOf:
7+
const arrayWithDuplicates = [1, 2, 3, 4, 2, 3, 5, 6, 1];
8+
const uniqueArray = arrayWithDuplicates.filter((value, index, self) => self.indexOf(value) === index);
9+
10+
console.log(uniqueArray); // Output: [1, 2, 3, 4, 5, 6]
11+
12+
//Using reduce:
13+
14+
const arrayWithDuplicates = [1, 2, 3, 4, 2, 3, 5, 6, 1];
15+
const uniqueArray = arrayWithDuplicates.reduce((accumulator, currentValue) => {
16+
if (!accumulator.includes(currentValue)) {
17+
accumulator.push(currentValue);
18+
}
19+
return accumulator;
20+
}, []);
21+
22+
console.log(uniqueArray); // Output: [1, 2, 3, 4, 5, 6]
23+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /