Home > other >  comparing elements in an array in JS
comparing elements in an array in JS

Time:01-13

How can I compare elements in an array like if I for example have arr=[1, 2, 3, 4, 4] and I wanna find out if there are any duplicates and remove them.Are there any fuctions that I can use for this?

CodePudding user response:

you can use a set data structure inbuilt into JS

let arr=[1, 2, 3, 4, 4] ;

let output = [...new Set(arr)]

CodePudding user response:

You need to convert the array to a set, A set has unique elements only as opposed to arrays that can have duplicates. To convert to a set

let arr=[1, 2, 3, 4, 4]
let set = new Set(arr);
  • Related