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?

1 Answers1

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))
Kinglish
  • 23,358
  • 3
  • 22
  • 43
  • Thanks! How about if I want to write a function that takes two arrays as input to do this ? – applefanboy Jul 12 '21 at 19:18
  • Not sure what you mean, this is taking 2 arrays as input. Can you update your question with what you mean + expected result? – Kinglish Jul 12 '21 at 19:29