0

I have a set of correct combinations like following

[["dbc", "dbs", "dbt"], ["dyc", "dys", "dyt"], ["drc", "drs", "drt"]]

and was hoping to see if my combinations contain any of correct combination when my combinations are

[["drs", "gbc", "lrs"], ["grt", "lbc", "lbt"], ["drc", "drs", "drt"], ["dyc", "dys", "dyt"]]

If there are any matches, then print out correct combination.

I've tested .contains, but I guess it only works with finding one element in one-dimensional array. How can I check if multi-dimensional array contains specific arrays?

2 Answers 2

2

So, in other terms, you have 2 lists of elements (where an element is an array of 3 strings) and you want to find the intersection of the 2 lists.

let corrects : Set = [["dbc", "dbs", "dbt"], ["dyc", "dys", "dyt"], ["drc", "drs", "drt"]]
let all : Set = [["drs", "gbc", "lrs"], ["grt", "lbc", "lbt"], ["drc", "drs", "drt"], ["dyc", "dys", "dyt"]]

let intersection = Array(corrects.intersect(all))

intersection // [["dyc", "dys", "dyt"], ["drc", "drs", "drt"]]

Hope this helps


P.S. The logic of my answer is similar of the one described by egor.zhdan. I'm just using the Swift native Set struct instead of the NSSet/NSMutableSet classes bridged from Objective-C.

Sign up to request clarification or add additional context in comments.

Comments

0

You can do it with NSMutableSet and intersectSet method:

let correct = [["dbc", "dbs", "dbt"], ["dyc", "dys", "dyt"], ["drc", "drs", "drt"]]
let all = [["drs", "gbc", "lrs"], ["grt", "lbc", "lbt"], ["drc", "drs", "drt"], ["dyc", "dys", "dyt"]]

let correctSet = NSSet(array: correct)
let allSet = NSMutableSet(array: all)
allSet.intersectSet(correctSet as Set<NSObject>)

print(allSet) // prints (("drc", "drs", "drt"), ("dyc", "dys", "dyt"))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.