0

Array1 = [1,2,3]

Array 2 = [2,4,5,7]

I have above two arrays , I need to find whether any value in Array1 is in another array (Array 2) .

If Array1(value) matches Array2(value){
   // BINGO , you have  integer value 2 in second array , i.e 2 is common in two arrays
}

I know contains() which checks if array contains particular value or not. But it would make more loops.

var elements = [1,2,3,4,5]
if elements.contains(5) {
   print("yes")
}

P.S:: I was searching if there is any swift function for this. I want to limit my loops for searching common values is two arrays.

Any help would be appreciated.

2

1 Answer 1

1

Don't use NSArray, use NSSet which doesn't contain duplicates, then if they're the same that means they have common values.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.