Find matching elements in array SWIFT 3 [duplicate]

99封情书 提交于 2019-12-13 03:47:12

问题


I have two arrays filled with strings and am hoping to identify the matching strings in both arrays. Eg.

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

I am wanting the outcome to be

["Dog", "Chicken"]

Thanks in advance


回答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)
    }
}


来源:https://stackoverflow.com/questions/45860944/find-matching-elements-in-array-swift-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!