Skip to content
Advertisement

Javascript check if any elements in array are equal

How should I make a function that loops through an array, and checks if any elements are equal

Example:

JavaScript

EDIT:

I want to use it with nested arrays, like so:

JavaScript

EDIT again: I want the nested arrays to be the exact same

JavaScript

Advertisement

Answer

You can easily do this using Set as:

JavaScript

You can add all elements to set and then compare size of set and length of arr.

You will get different length if any element is repeated else same

JavaScript

If you want to use nested array then you can flat it before seinding arguments to getResult function as:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement