Swift - Determine if Array1 contains at least one object from Array2

后端 未结 6 863
清歌不尽
清歌不尽 2020-12-28 21:11

I have 2 Arrays. Say, array1 = [1,2,3,4,5] and array2 = [2,3]. How could I check in swift if array1 contains at least one item from

6条回答
  •  遥遥无期
    2020-12-28 21:18

    An alternative way would be using Sets:

    let array1 = [1,2,3,4,5]
    let array2 = [2,3]
    let set1 = Set(array1)
    let intersect = set1.intersect(array2)
    
    if !intersect.isEmpty {
        // do something with the intersecting elements
    }
    

提交回复
热议问题