【问题标题】:How can I merge arrays with no duplicates when they have elements in common当它们有共同的元素时,如何合并没有重复的数组
【发布时间】:2020-10-14 12:48:45
【问题描述】:

Swift - 当数组有共同的元素时,如何合并没有重复的数组? 假设我有一个这样的数组数组

[[1, 2, 3], [4, 3, 5], [7], [6, 8, 7], [9]]

并且我想在它们有共同元素时合并所有没有重复的数组。所以输出会是这样的:

[[1, 2, 3, 4, 5], [7, 6, 8], [9]]

前两个数组共有 3 个,因此它们被合并,第二个和第三个数组共有 7 个,依此类推。

有什么方法可以快速做这样的方法吗?

【问题讨论】:

  • 是的,在 swift 中绝对可以,但是没有内置函数可以做到这一点,因此您必须自己编写代码。
  • 使用 Array.contains(_:)。
  • 输入[[1, 2, 3], [4, 3, 5], [7], [6, 8, 7, 3], [9]]想要什么结果?
  • @pawello2222 [[1, 2, 3, 4, 5, 6, 7, 8], [9]]
  • “有什么办法……”,你的研究在哪里,你在哪里尝试解决这个问题?这不是免费的代码编写服务。

标签: ios arrays swift arraylist swift3


【解决方案1】:

让我们试试:

func merge(array: [[Int]]) -> [Set<Int>] {
    
    let setList = array.map({ Set($0) })
    let flags = setList.enumerated().map { (index, set) -> [Bool] in
        return (0..<setList.count).map({ counterIndex in
            return counterIndex != index && !set.intersection(setList[counterIndex]).isEmpty
        })
    }
    
    var processedIndex = Set<Int>()
    
    var result = [Set<Int>]()
    for i in 0..<array.count {
        guard !processedIndex.contains(i) else {
            continue
        }
        result.append(merge(setList: setList, with: flags, currentIndex: i, processedIndex: &processedIndex))
    }
    
    return result
}

func merge(setList: [Set<Int>], with flags: [[Bool]], currentIndex: Int, processedIndex: inout Set<Int>) -> Set<Int> {
    guard !processedIndex.contains(currentIndex) else {
        return []
    }
    
    var rs = setList[currentIndex]
    processedIndex.insert(currentIndex)
    
    for i in currentIndex..<setList.count where !processedIndex.contains(i) && flags[currentIndex][i] {
        rs.formUnion(
            merge(setList: setList, with: flags, currentIndex: i, processedIndex: &processedIndex)
        )
    }
    
    return rs
}

【讨论】:

  • 当数组排列为不同的 [[1, 2, 3], [7], [4, 3, 5], [6, 8, 7], [9] 时,此解决方案将失败]]
  • 输入 [[1, 2, 3], [4, 3, 5], [6, 8, 7, 3], [9], [7]] ,预期输出?
  • [[1, 2, 3, 4, 5, 6, 8, 7], [9]] 我需要合并具有共同元素的数组在一个数组中没有重复。订购不是强制性的
【解决方案2】:

您可以尝试以下方法:

let input = [[1, 2, 3], [4, 3, 5], [7], [6, 8, 7], [9]]
var result = [[Int]]()

input.forEach { item in
    // indicates whether we added an item in this iteration
    var itemAdded = false
    result.forEach { oldItem in
        // check if both items intersect
        if !oldItem.filter(item.contains).isEmpty {
            // create a new item without duplicates
            let newItem: [Int] = Array(Set(oldItem + item))
            // remove the old item in the result...
            result.remove(at: result.firstIndex(of: oldItem)!)
            // ...and replace with a merged one (if wasn't added before)
            if !itemAdded {
                result.append(newItem)
            }
            itemAdded = true
        }
    }
    // if we didn't add any item in this iteration (found no intersections) add the item
    if !itemAdded {
        result.append(item)
    }
}

print(result) // prints [[1, 5, 2, 4, 3], [7, 8, 6], [9]]

如果您想订购您的结果,您可以使用.sorted():

let newItem: [Int] = Array(Set(oldItem + item)).sorted()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 2017-04-18
    • 1970-01-01
    • 1970-01-01
    • 2015-05-19
    相关资源
    最近更新 更多