Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Print all unique pairs of a string array without repeating on other side in Swift

Have searched all stackoverflow, couldn’t find answer for my particular question-

Array is
["Codex1", "Codex2", "Codex3"]

Requirement is
["Codex1", "Codex2"] , ["Codex2", "Codex3"] , ["Codex1", "Codex3"]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Or if elements are 4,
["Codex1", "Codex2", "Codex3", "Codex4"]

should print
["Codex1", "Codex2"] , ["Codex3", "Codex4"] , ["Codex1", "Codex3"], ["Codex1", "Codex4"],["Codex2", "Codex3"], ["Codex2", "Codex4"]

and similarly so on. Its not a requirement that it should print in [String, String] , it can be other data structure.

>Solution :

You can try

let arr = ["Codex1", "Codex2", "Codex3", "Codex4"]
var res = [[String]]()
arr.indices.forEach { item in
    ((item+1)..<arr.count).forEach {
        res.append([arr[item],arr[$0]])
    }
}
print(res)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading