JS Program to Remove Duplicates From Array

hannaha88

hannaA

Posted on July 30, 2022

JS Program to Remove Duplicates From Array

solution1

let array = ['1', '2', '3', '2', '3'];

let uniqueArray = [];
array.forEach((c) => {
    if (!uniqueArray.includes(c)) {
        uniqueArray.push(c);
    }
});

console.log(uniqueArray);
Enter fullscreen mode Exit fullscreen mode

solution2

function getUnique(arr){

    let uniqueArr = [];

    //loop through array 
    for(let i of arr) {
        if(uniqueArr.indexOf(i) === -1) {
            uniqueArr.push(i);
        }
    }
    console.log(uniqueArr);
}
const array = [1,2,3,2,3];

Enter fullscreen mode Exit fullscreen mode

solution3

let array = ['1', '2', '3', '2', '3'];
let uniqueArray = [...new Set(array)];

console.log(uniqueArray);
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
hannaha88
hannaA

Posted on July 30, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related