Skip to content

Instantly share code, notes, and snippets.

@mmloveaa
Last active December 27, 2015 19:17
Show Gist options
  • Save mmloveaa/a3e65426f7d4270e1a89 to your computer and use it in GitHub Desktop.
Save mmloveaa/a3e65426f7d4270e1a89 to your computer and use it in GitHub Desktop.
Find unique items of an array
// 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