There are two methods to remove duplicate content from an array such as using a temporary array and a separate index.

1. Using indexOf() Method

const getUnique = (arr) => {     
    let uniqueArr = [];    
     for(let i of arr) {
        if(uniqueArr.indexOf(i) === -1) {
            uniqueArr.push(i);
        }
    }
    console.log(uniqueArr);
}
const array = [1, 2, 3, 2, 3,4,5];
getUnique(array);

Answer

[ 1, 2, 3, 4, 5 ]

2. Using Set Method

let chars = ['A', 'B', 'A', 'C', 'B'];
let uniqueChars = [...new Set(chars)];
console.log(uniqueChars);

Answer

[ 'A', 'B', 'C' ]

BY Best Interview Question ON 24 Aug 2022

Suggest An Answer