0

I want to check if two arrays have non-repeating identical elements.

const first = [1,2,3,4]
const second = [1,2]

const result = second.every(element => first.includes(element))
//Result: true

But this code doesn't work with duplicated elements. For example:

const first = [1,2,3,4]
const second = [3,3]

const result = second.every(element => first.includes(element))
//Result: true //MUST BE false

How can I solve it? Please help!

  • 1
    Check if an array has duplicate values: https://stackoverflow.com/questions/19655975/check-if-an-array-contains-duplicate-values – kelsny Sep 06 '22 at 20:12
  • 2
    What is "non-repeating identical elements" ? to me 1,2,3,4 don't look identical to 1,2 – Alejandro Montilla Sep 06 '22 at 20:16
  • Alejandro, I wrote a question with google translate, maybe in the print) I mean in array [1,2,3,4] has values [1,2], bun in [1,2,3,4] hasn't values [3,3] – Baglan Abdirassil Sep 06 '22 at 20:29

1 Answers1

2
const result = second.every(element => first.includes(element) && second.indexOf(element) !== second.lastIndexOf(element))
adsy
  • 8,531
  • 2
  • 20
  • 31