Skip to content

Commit

Permalink
Added unique function
Browse files Browse the repository at this point in the history
  • Loading branch information
Reetam101 committed Oct 9, 2021
1 parent ec0c7dc commit e5b16c0
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions unique.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const unique = (arr) => {
const prims = {"boolean":{}, "number":{}, "string":{}}, objects = [];

return arr.filter((item) => {
const type = typeof item;
if(type in prims)
return prims[type].hasOwnProperty(item) ? false : (prims[type][item] = true);
else
return objects.indexOf(item) >= 0 ? false : objects.push(item);
});
}

console.log(unique(["apple", "orange", "apple", 1, 5, 1, 1, 2]));
// output - [ 'apple', 'orange', 1, 5, 2 ]

0 comments on commit e5b16c0

Please sign in to comment.