Last active
March 23, 2023 06:49
-
-
Save ZacharyL2/25cfae05fc88d8aeb4b08d16a880106d to your computer and use it in GitHub Desktop.
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
// π Solution 1 | |
function unique(arr) { | |
const result = []; | |
for (const i of arr) { | |
let noRepeat = true; | |
for (const j of result) { | |
if (i === j) { | |
noRepeat = false; | |
break; | |
} | |
} | |
if (noRepeat) { | |
result.push(i); | |
} | |
} | |
return result; | |
} | |
// π Solution 2 | |
function unique(arr) { | |
const result = []; | |
for (const item of arr) { | |
// π Option 1 | |
if (!result.includes(item)) { | |
result.push(item); | |
} | |
// π Option 2 | |
// if (result.indexOf(item) === -1) { | |
// result.push(item); | |
// } | |
} | |
return result; | |
} | |
function unique(arr) { | |
const result = arr.reduce((previousValue, currentValue) => { | |
// π Option 1 | |
if (!previousValue.includes(currentValue)) { | |
previousValue.push(currentValue); | |
} | |
// π Option 2 | |
// if (previousValue.indexOf(currentValue) === -1) { | |
// previousValue.push(currentValue); | |
// } | |
return previousValue; | |
}, []); | |
return result; | |
} | |
// π Solution 3 | |
function unique(arr) { | |
const result = arr.filter((element, index) => arr.indexOf(element) === index); | |
return result; | |
} | |
// π Solution 4 | |
function unique(arr) { | |
arr.sort((a, b) => a - b); | |
let result = []; | |
// π Option 1 | |
for (let i = 0; i < arr.length; i += 1) { | |
if (arr[i] !== arr[i - 1]) { | |
result.push(arr[i]); | |
} | |
} | |
// π Option 2 | |
// result = arr.reduce((previousValue, currentValue, currentIndex) => { | |
// if (arr[currentIndex] !== arr[currentIndex - 1]) { | |
// previousValue.push(currentValue); | |
// } | |
// return previousValue; | |
// }, []); | |
// π Option 3 | |
// result = arr.filter((_, index) => arr[index] !== arr[index - 1]); | |
return result; | |
} | |
// π Solution 5 | |
function unique(arr) { | |
const map = new Map(); | |
let result = []; | |
// π Option 1 | |
for (const item of arr) { | |
if (!map.has(item)) { | |
result.push(item); | |
map.set(item, true); | |
} | |
} | |
// π Option 2 | |
// result = arr.reduce((previousValue, currentValue) => { | |
// if (!map.has(currentValue)) { | |
// previousValue.push(currentValue); | |
// map.set(currentValue, true); | |
// } | |
// return previousValue; | |
// }, []); | |
// π Option 3 | |
// result = arr.filter((element) => { | |
// if (!map.has(element)) { | |
// map.set(element, true); | |
// return true; | |
// } | |
// return false; | |
// }); | |
return result; | |
} | |
// π Solution 6 | |
function unique(arr) { | |
// π Option 1 | |
return [...new Set(arr)]; | |
// π Option 2 | |
// return Array.from(new Set(arr)); | |
} | |
// π | |
console.log(unique([1, 1, 3, 2, 2])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment