How to remove duplicate values from a JavaScript array?

devquora
devquora

Posted On: Feb 22, 2018

 

We can use array.indexOf method to check a value exists or not. See below example to remove duplicate values.

let duplicates = ['delhi','kanpur','kanpur','goa','delhi','new york'];

function removeDuplicatesValues(arr){
    let unique_array = [];
    for(let i = 0;i < arr.length; i++){
        if(unique_array.indexOf(arr[i]) == -1){
            unique_array.push(arr[i])
        }
    }
    return unique_array
}

console.log(removeDuplicatesValues(duplicates));

    Related Questions

    Please Login or Register to leave a response.

    Related Questions

    JavaScript Interview Questions

    Explain what is Javascript?

    JavaScript, a dynamic, client-side language, is crucial in modern web development, working alongside HTML and CSS. Its manipulation of the Document Object Model (DOM) enables dynamic content and inter..

    JavaScript Interview Questions

    What close() does in Javascript?

    In JavaScript, the close() method is employed to securely close the current window. To ensure its association with the window object rather than another JavaScript object, employ window.close(). This ..

    JavaScript Interview Questions

    What is the difference between let and var?

    Both var and let are utilized for variable/method declaration in JavaScript. The crucial disparity lies in their scope: var is function-scoped, meaning it's accessible throughout the function it's def..