Remove duplicates from an array in JavaScript

JavaScript

To day we will discuss about how to remove duplicates from an array in JavaScript. There are several methods to remove duplicates from an array in JavaScript. These methods are :-

  1. Set()
  2. Filter() & indexOf()
  3. forEach() & includes()
  4. for() & indexOf()
  5. Reduce()

Set()

Basically Set() is a special object type in JavaScript and introduced in ES6. It is collection of unique values. We will discuss later about Set() in details.

const arr = ['python', 'java', 'javascript', 'python', 'HTML', 'javascript'];

const uniqueArr = [...new Set(arr)];

console.log(uniqueArr);

// Output

['python', 'java', 'javascript', 'HTML']

So in the above code we have used Set() method to remove duplicate values from an array.

Filter() & indexOf()

Basically filter() creates a new array with those element which satisfy the given condition in array.

indexOf is used to find the first occurrence of the item in string. It returns -1 if item doesn’t exist in string. If the item is exist more than one in string then it returns the position of the first occurrence of item.

const arr = ['python', 'java', 'javascript', 'python', 'HTML', 'javascript'];

const uniqueArr = arr.filter((item, index) => {

    return arr.indexOf(item) === index;

});

console.log(uniqueArr);

// Output

['python', 'java', 'javascript', 'HTML']

forEach() & includes()

.forEach() executes a provided function once for each element of an array and does not returns anything i.e. it returns undefined.

includes() method is used to find out existencey of item in array. It returns boolean value. If item is exist in array then it returns True otherwise False. We will discuss later about includes() in javascript.

const arr = ['python', 'java', 'javascript', 'python', 'HTML', 'javascript'];

const uniqueArr = [];

arr.forEach((item) => {

    if (!uniqueArr.includes(item)) {

        uniqueArr.push(item);

    }

});

console.log(uniqueArr);

// Output

['python', 'java', 'javascript', 'HTML']

For() & indexOf()

Basically for() loop is used to iterate the array in JavaScript and indexOf is used to find the first occurrence of the item in string.

const arr = ['python', 'java', 'javascript', 'python', 'HTML', 'javascript'];

const uniqueArr = [];

for (let i = arr.length - 1; i >= 0; i--) {

    if (uniqueArr.indexOf(arr[i]) === -1) {

        uniqueArr.push(arr[i])

    }

}

console.log(uniqueArr);

// Output

['python', 'java', 'javascript', 'HTML']

Reduce()

Basically reduce() method is used to reduce an array in to single item. We will discuss later in details about reduce() method.

const arr = ['python', 'java', 'javascript', 'python', 'HTML', 'javascript'];

const uniqueArr = arr.reduce(function(a, b) {

    if (a.indexOf(b) < 0) {

        a.push(b)

    }

    return a;

}, [])

console.log(uniqueArr);

// Output

['python', 'java', 'javascript', 'HTML']

So in this tutorial we have discussed about various methods to remove duplicates from an array in JavaScript. Using this tutorial anyone can easily understand, how to remove duplicates from an array in JavaScript.

If you want to improve this tutorial or find anything wrong or incorrect then please leave a message using below comment box.

1 thought on “Remove duplicates from an array in JavaScript

Leave a Reply

Your email address will not be published. Required fields are marked *

ten − three =