728x90
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Filter unique array members | |
// importance: 4 | |
// Let arr be an array. | |
// Create a function unique(arr) that should return an array with unique items of arr. | |
// For instance: | |
function unique(strArr) { | |
const resultArr = []; | |
for (let str of strArr) { | |
if(!resultArr.includes(str)) { | |
resultArr.push(str); | |
} | |
} | |
return resultArr; | |
} | |
let strings = ["Hare", "Krishna", "Hare", "Krishna", | |
"Krishna", "Krishna", "Hare", "Hare", ":-O" | |
]; | |
console.log( unique(strings) ); // Hare, Krishna, :-O |
'Algorithm > JavaScript(Node.js)' 카테고리의 다른 글
The Modern JS Tutorial: Map and Set: filter unique array members (0) | 2021.10.06 |
---|---|
The Modern JS Tutorial: array-methods: Create keyed object from array (0) | 2021.09.23 |
The Modern JS Tutorial: array-methods: Get average age (0) | 2021.09.22 |
The Modern JS Tutorial: array-methods: shuffle an array (0) | 2021.09.21 |
The Modern JS Tutorial: array-methods: sort users by age (0) | 2021.09.20 |