javascript
Remove duplicates from an array and return unique values in O(n) complexity
let arr = [1, 2, 3, 4, 3, 7, 6, 5, 4];
function unique(arr) {
let items = {};
arr.forEach((item) => {
if (!items[item]) {
items[item] = item;
}
});
return items;
}
console.log(Object.values(unique(arr)));
Output
[1,2,3,4,5,6,7]
Was this helpful?
Similar Posts
- Lodash - merge two arrays and remove duplicates from it
- Remove duplicates ids from object array
- Collect books from array of objects and return collection of books as an array
- Remove all false, null, undefined and empty values from array using Lodash
- Remove empty, false, undefined values from array Javscript
- Remove duplicate values from an array in Javascript
- Random & Unique CSS Gradient-Color Generator