2015-09-04 55 views
0

數組1 = [1,2,3]如何檢查兩個數組中是否有共同的值 - IN SWIFT?

陣列2 = [2,4,5,7-]

我具有上述兩個數組,我需要找到在數組1的任何值是否在另一個數組(數組2)。

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

我知道包含()檢查數組是否包含特定值。但它會造成更多的循環。

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

P.S ::我在搜索是否有任何這樣的快捷功能。我想限制搜索公共值的循環是兩個數組。

任何幫助,將不勝感激。

+8

可能重複的[在Swift數組上設置運算(union,intersection)?](http://stackoverflow.com/questions/24589181/set-operations-union-intersection-on-swift-array) – Larme

+1

檢查這個鏈接http://stackoverflow.com/questions/32065941/how-to-compare-2-arrays-in-2-columns-of-a-picker-view-and-display-the-result-in/32066154#32066154 –

回答

1

請勿使用NSArray,請使用不包含重複項的NSSet,如果它們相同,則表示它們具有共同的值。