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.

user3804063
  • 809
  • 1
  • 14
  • 32
  • 8
    possible duplicate of [Set operations (union, intersection) on Swift array?](http://stackoverflow.com/questions/24589181/set-operations-union-intersection-on-swift-array) – Larme Sep 04 '15 at 09:11
  • 1
    check this link http://stackoverflow.com/questions/32065941/how-to-compare-2-arrays-in-2-columns-of-a-picker-view-and-display-the-result-in/32066154#32066154 – Pallavi Konda Sep 04 '15 at 09:12

1 Answers1

1

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

Pablo A.
  • 2,042
  • 1
  • 17
  • 27