Photo by Stefan Cosma on Unsplash
JavaScript: Remove duplicates from array without using any shortcut methods
Table of contents
No headings in the article.
let arr = [2, 1, 3, 1, 7, 1, 7, 9, 3];
function removeDuplicates(arr) {
let obj = {};
let arrWithNoDuplicates = [];
for (let i = 0; i < arr.length; i++) {
obj[arr[i]] = true;
}
for (let key in obj) {
arrWithNoDuplicates.push(key);
}
return arrWithNoDuplicates;
}
console.log(removeDuplicates(arr));
Output:
[ '1', '2', '3', '7', '9' ]