Last active
December 27, 2015 19:17
-
-
Save mmloveaa/a3e65426f7d4270e1a89 to your computer and use it in GitHub Desktop.
Find unique items of an array
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
// 12/22/2015 | |
// Write a function uniq that takes in an array arr then returns | |
// unique items of arr. | |
// For example | |
// uniq(['a', 'b', 'a']) === ['a', 'b'] | |
// My Solution: | |
function uniq(arr) { | |
var arr1=[]; | |
for (var i=0; i<arr.length; i++){ | |
if (arr1.indexOf(arr[i])==-1){ | |
arr1.push(arr[i]) | |
} | |
} | |
return arr1; | |
} | |
uniq(['a', 'b', 'a']) | |
// Other's solution | |
// function uniq(arr) { | |
// var res = []; | |
// arr.forEach(function(item) { | |
// if (res.indexOf(item) === -1) { | |
// res.push(item) | |
// } | |
// }) | |
// return res; | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment