0

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?

Lunste
  • 11
  • 1

2 Answers2

2

you can use a set data structure inbuilt into JS

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

let output = [...new Set(arr)]
ashish singh
  • 6,526
  • 2
  • 15
  • 35
0

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);
Habeeb Tijani
  • 1
  • 3
  • 14