2017-08-24 97 views
0

我有兩個數組填充字符串,希望能夠識別兩個數組中的匹配字符串。例如。查找數組中的匹配元素SWIFT 3

var ArrayOne = ["Dog", "Cat", "Chicken"] 
var Array Two = ["Dog", "Elephant", "Chicken", "Sheep"] 

我想要的結果是提前

+0

的可能的複製https://stackoverflow.com/questions/24589181/set-operations-union

["Dog", "Chicken"] 

謝謝-In-swift-array –

+1

'let matching = Set(ArrayOne).intersection(ArrayTwo)' – vacawama

回答

1
var ArrayOne = ["Dog", "Cat", "Chicken"] 
var ArrayTwo = ["Dog", "Elephant", "Chicken", "Sheep"] 

var ArrayThree = [String]() 
for animal in ArrayOne { 
    if ArrayTwo.contains(animal) { 
     ArrayThree.append(animal) 
    } 
}