How to get non duplicate values from array in javascript

Let's check, many times (or few) arises the need to remove duplicate elements given in an array, I don't know... it can be because you have to print a list from the super, remove a student that duplicated his record in a form, infinity of things, so let's see some ways to do this:

1) Use Set

Using Set(), an instance of unique values will be created, implicitly using this instance will delete the duplicates.

So we can make use of this instance and from there we will have to convert that instance into a new array, and that would be it:

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

Enter fullscreen mode Exit fullscreen mode

Output:

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

Enter fullscreen mode Exit fullscreen mode

2) Using the indexOf() and filter() methods

The indexOf() method returns the index of the first occurrence of the element in the array:

let chars = ['A', 'B', 'A', 'C', 'B']; chars.indexOf('B');

Enter fullscreen mode Exit fullscreen mode

Output:

1

Enter fullscreen mode Exit fullscreen mode

The duplicate element is the element whose index is different from its indexOf() value:

let chars = ['A', 'B', 'A', 'C', 'B']; chars.forEach((element, index) => { console.log(`${element} - ${index} - ${chars.indexOf(element)}`); });

Enter fullscreen mode Exit fullscreen mode

Output:

A - 0 - 0 B - 1 - 1 A - 2 - 0 C - 3 - 3 B - 4 - 1

Enter fullscreen mode Exit fullscreen mode

To eliminate duplicates, the filter() method is used to include only the elements whose indexes match their indexOf values, since we know that the filer method returns a new array based on the operations performed on it:

let chars = ['A', 'B', 'A', 'C', 'B']; let uniqueChars = chars.filter((element, index) => { return chars.indexOf(element) === index; }); console.log(uniqueChars);

Enter fullscreen mode Exit fullscreen mode

Output:

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

Enter fullscreen mode Exit fullscreen mode

And if by chance we need the duplicates, we can modify our function a little, just by changing our rule:

let chars = ['A', 'B', 'A', 'C', 'B']; let dupChars = chars.filter((element, index) => { return chars.indexOf(element) !== index; }); console.log(dupChars);

Enter fullscreen mode Exit fullscreen mode

Output:

['A', 'B']

Enter fullscreen mode Exit fullscreen mode

3) Using the includes() and forEach() methods

The include() function returns true if an element is in an array or false if it is not.

The following example iterates over the elements of an array and adds to a new array only the elements that are not already there:

let chars = ['A', 'B', 'A', 'C', 'B']; let uniqueChars = []; chars.forEach((element) => { if (!uniqueChars.includes(element)) { uniqueChars.push(element); } }); console.log(uniqueChars);

Enter fullscreen mode Exit fullscreen mode

Output:

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

Enter fullscreen mode Exit fullscreen mode

Basically, we have options to solve this type of problem, so don't get stuck anymore and you can use whichever one appeals to you.


If you liked the content you can follow me on my social networks as @soyleninjs

How do you find unique values in an array?

With JavaScript 1.6 / ECMAScript 5 you can use the native filter method of an Array in the following way to get an array with unique values:.
function onlyUnique(value, index, self) { return self. ... .
// usage example: var myArray = ['a', 1, 'a', 2, '1']; var unique = myArray..

How do I find the same element in an array?

Algorithm.
Declare and initialize an array..
Duplicate elements can be found using two loops. The outer loop will iterate through the array from 0 to length of the array. The outer loop will select an element. ... .
If a match is found which means the duplicate element is found then, display the element..

How do you make an array unique?

Minimum Increment operations to make Array unique..
Making elements distinct in a sorted array by minimum increments..
Find sum of non-repeating (distinct) elements in an array..
Find K closest elements to given Value in Unsorted Array..
Find k closest elements to a given value..
Search in an almost sorted array..

How do you remove duplicates from array of objects in JS?

To remove the duplicates from an array of objects: Use the Array. filter() method to filter the array of objects. Only include objects with unique IDs in the new array.

Chủ đề