Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
920 views
in Technique[技术] by (71.8m points)

compare multiple arrays for same elements in swift

I'm new to swift and programming in general. I have multiple arrays of names in a database and I need to check for same names in these arrays. I've found some solutions to compare two arrays, but not multiple so I wrote some additional code. But performance wise it's not the best practice I think. And also not the best way to add first all the names and then remove the duplicates..

Does anyone has any better ideas/solutions for my problem?

Code:

import UIKit

let array1 = ["Max", "Peter","Kathrin", "Sara", "Kirsten", "Mike", "Elon"]      // Peter, Kathrin, Mike, Sara
let array2 = ["Pamela", "Chris", "James", "Sebastian", "Mike"]                  // Mike, Chris
let array3 = ["John", "Daniel", "Susan", "Mathias", "Mike", "Donald"]           // Mike
let array4 = ["Tim", "Kathrin", "Alan", "Chris", "Amy", "Sara"]                 // Kathrin, Chris
let array5 = ["Cara", "Charly", "Emily", "Maja", "Peter", "Sara"]               // Peter, Sara
// Output should be: Peter, Kathrin, Mike, Sara, Chris

var array = [Array<String>]()
array.append(array1)
array.append(array2)
array.append(array3)
array.append(array4)
array.append(array5)

var names = [String]()

for i in 0...array.count - 2 {
    for z in 1...array.count - 1 {
        if z + i < array.count {
            let commonElements = Array(Set(array[i]).intersection(Set(array[z+i])))
            names.append(contentsOf: commonElements)
        }
    }
}

print(names.removeDuplicates())

Extension:

extension Array where Element: Hashable {
    func removeDuplicates() -> [Element] {
        var result = [Element]()

        for value in self {
            if result.contains(value) == false {
                result.append(value)
            }
        }
        return result
    }
}
question from:https://stackoverflow.com/questions/65646309/compare-multiple-arrays-for-same-elements-in-swift

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If your intent is to just check if a name occurs in more than one collection I think the best way to approach this is creating a single collection with all the names and filter the duplicates as shown in this post

let array1 = ["Max", "Peter","Kathrin", "Sara", "Kirsten", "Mike", "Elon"]
let array2 = ["Pamela", "Chris", "James", "Sebastian", "Mike"]
let array3 = ["John", "Daniel", "Susan", "Mathias", "Mike", "Donald"]
let array4 = ["Tim", "Kathrin", "Alan", "Chris", "Amy", "Sara"]
let array5 = ["Cara", "Charly", "Emily", "Maja", "Peter", "Sara"]

var names: [String] = []
names.append(contentsOf: array1)
names.append(contentsOf: array2)
names.append(contentsOf: array3)
names.append(contentsOf: array4)
names.append(contentsOf: array5)

extension RangeReplaceableCollection where Element: Hashable {
    var duplicates: Self {
        var set: Set<Element> = []
        var filtered: Set<Element> = []
        return filter { !set.insert($0).inserted && filtered.insert($0).inserted }
    }
}

// Output should be: Peter, Kathrin, Mike, Sara, Chris
print(names.duplicates)  // ["Mike", "Kathrin", "Chris", "Sara", "Peter"]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...