在数组SWIFT 3中查找匹配元素

时间:2017-08-24 11:54:12

标签: arrays swift swift3

我有两个字符串填充字符串,我希望识别两个数组中的匹配字符串。例如

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

我希望结果是

["Dog", "Chicken"]

提前致谢

1 个答案:

答案 0 :(得分: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)
    }
}