0

I want to create a function that can check if 2 arrays are subsets or not, and if yes return true else return false

For example if : x = ["BOB","ADA","KEN"] y = ["KEN", "BOB"] return true

x = ["BOB","ADA","KEN"] y = ["KEN", "BOB" , "DAN"] return false

Any suggestions on how to create this function?

0

1 Answer 1

1

You can filter the second array by the first to see if there's anything left afterwards. If so, they don't overlap.

const hasOverlap = (arr1, arr2) => arr2.filter(a => !arr1.includes(a)).length === 0

let x = ["BOB","ADA","KEN"], y = ["KEN", "BOB" ] 
console.log(hasOverlap(x,y))

x = ["BOB","ADA","KEN"]
y = ["KEN", "BOB", "JOE"] 
console.log(hasOverlap(x,y))

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! How about if I want to write a function that takes two arrays as input to do this ?
Not sure what you mean, this is taking 2 arrays as input. Can you update your question with what you mean + expected result?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.