Last active
August 15, 2017 23:55
-
-
Save twmulloy/3eef68759df1cdf6a791d795e44ba03b 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
// O(n^2) | |
function unique(arr) { | |
var result = []; | |
for(var i = 0; i < arr.length; i++) { | |
if (result.indexOf(arr[i]) === -1) { | |
result.push(arr[i]); | |
} | |
} | |
return result; | |
} | |
console.log('output ', unique([ | |
'fizz', 'bizz', 'fuzz', 'buzz', 'fuzz', 'bizz' | |
])) | |
// output [ 'fizz', 'bizz', 'fuzz', 'buzz' ] |
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
// O(n) | |
function unique(arr) { | |
var result = {}; | |
for(var i = 0; i < arr.length; i++) { | |
result[arr[i]] = true; | |
} | |
return Object.keys(result); | |
} | |
console.log('output ', unique([ | |
'fizz', 'bizz', 'fuzz', 'buzz', 'fuzz', 'bizz' | |
])) | |
// output [ 'fizz', 'bizz', 'fuzz', 'buzz' ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment