Baglan Abdirassil
Baglan Abdirassil

Reputation: 13

Check if two arrays have non-repeating identical elements

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!

Upvotes: 0

Views: 102

Answers (1)

adsy
adsy

Reputation: 11382

const result = second.every(element => first.includes(element) && second.indexOf(element) !== second.lastIndexOf(element))

Upvotes: 2

Related Questions