Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if one array contains all elements of another array in Swift?

Tags:

arrays

ios

swift

I have 2 arrays:

var list:Array<Int> = [1,2,3,4,5]
var findList:Array<Int> = [1,3,5]

I want to determine if list Array contains all findList elements.

By the way, elements might be String as well or other type.

How to do that?

I know that Swift provides contains method that works with one item.

like image 513
Maxim Shoustin Avatar asked Sep 07 '14 21:09

Maxim Shoustin


People also ask

How do you check if an array contains all the elements of another array?

You can try with Array. prototype. every() : The every() method tests whether all elements in the array pass the test implemented by the provided function.

How do I compare two array elements in Swift?

To check if two arrays are equal in Swift, use Equal To == operator. Equal To operator returns a Boolean value indicating whether two arrays contain the same elements in the same order. If two arrays are equal, then the operator returns true , or else it returns false .

How do you check if an array contains an element in Swift?

The contains() method returns: true - if the array contains the specified element. false - if the array doesn't contain the specified element.


3 Answers

Instead of iterating through arrays and doing filtering yourself, you can use NSSet to do all the work for you.

var list:Array<Int> = [1,2,3,4,5]
var findList:Array<Int> = [1,3,5]

let listSet = NSSet(array: list)
let findListSet = NSSet(array: findList)

let allElemtsEqual = findListSet.isSubsetOfSet(otherSet: listSet)

NSSet is a lot faster than arrays at checking if it contains any object. In fact it's what it's designed for.

Edit: Using Swift's built-in Set.

let list = [1,2,3,4,5]
let findList = [1,3,5]
let listSet = Set(list)
let findListSet = Set(findList)
//**Swift 4.2 and Above**
let allElemsContained = findListSet.isSubset(of: listSet)

//below versions
//let allElemsContained = findListSet.isSubsetOf(listSet)
like image 184
orkoden Avatar answered Oct 21 '22 20:10

orkoden


allSatisfy seems to be what you want, assuming you can't conform your elements to Hashable and use the set intersection approach others have mentioned:

let containsAll = subArray.allSatisfy(largerArray.contains)
like image 36
Jacob Relkin Avatar answered Oct 21 '22 19:10

Jacob Relkin


Since Swift 4.2 you can write:

extension Array where Element: Equatable {
    func satisfy(array: [Element]) -> Bool {
        return self.allSatisfy(array.contains)
    }
}

Otherwise for Swift 3, Swift 4 you can write this:

extension Array where Element: Equatable {
    func contains(array: [Element]) -> Bool {
        for item in array {
            if !self.contains(item) { return false }
        }
        return true
    }
}

You can see the:

  • contains method here
  • allSatisfy method here

This is just a simple extension that check if the array that you give is in the current array (self)

like image 12
Julien Kode Avatar answered Oct 21 '22 20:10

Julien Kode