JavaScript: Remove duplicates from array without using any shortcut methods

1 min read
Table of contents

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' ]
0
Subscribe to my newsletter
Read articles from Hari Krishna Anem directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Hari Krishna Anem
Hari Krishna Anem
Full stack developer (ReactJS, NodeJS, JavaScript, PHP, SQL)