Search code examples
arraysswiftswift3

Find matching elements in array Swift 3


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


Solution

  • 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)
        }
    }