快速-性能明智-比较两个数组并获得两者的差异 [英] Swift - performance wise - comparing two arrays and get the difference in each and common in both

查看:137
本文介绍了快速-性能明智-比较两个数组并获得两者的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望您过得愉快.

我试图了解什么是执行以下操作的最快方法:

I'm trying to understand what is the fastest approach to do the following:

假设我有这两个Arrays:

var firstArray = ["a","b","c"]
var secondArray = ["a","d","e"]

我想作为输出:

1)Array位于内部 firstArray 但不位于 secondArray中的对象.
1)Array位于内部 secondArray 但不位于 firstArray中的对象.
3)firstArraysecondArray之间的常见对象中的Array.

1)Array of objects that inside firstArray but no in secondArray .
1)Array of objects that inside secondArray but no in firstArray .
3)Array of the common objects between firstArray and secondArray.

所以基本上输出是:

1)["b","c"]
2)["d","e"]
3)["a"]

1) ["b","c"]
2) ["d","e"]
3) ["a"]

这里的主要问题是了解什么是最有效的方法.非常感谢你!

The main issue here is to understand what is the most efficient way to do so. Thank you very much!

推荐答案

如果对数组进行排序并且每个数组中的项都是唯一的,则最快的方法是只处理每个项一次.首先比较每个数组中的第一项;如果它们相等,则将其放入公共数组,然后移至第二项.如果一项少于另一项,它将进入小项的唯一数组,然后移至小项数组中的下一项.继续此过程,直到一个阵列的项目用完,然后将第二个阵列的其余项目放入该阵列的唯一项目阵列中.

If your arrays are sorted and the items are unique in each array, the fastest way would be to handle each of the items only once. Start by comparing the first items in each array; if they are equal, put that into the common array and then move on to the second items. If one item is less than another, it goes into the unique array of the lesser item and you move on to the next item in the lesser array. Continue this process until you run out of items for one array, then put the remaining items of the second array into the unique items array for that array.

var i = 0
var j = 0

let a = ["a", "b", "c"]
let b = ["a", "d", "e"]

var aUnique = [String]()
var bUnique = [String]()
var common = [String]()

while i < a.count && j < b.count {
    if a[i] == b[j] {
        common.append(a[i])
        i += 1
        j += 1
    } else if a[i] < b[j] {
        aUnique.append(a[i])
        i += 1
    } else {
        bUnique.append(b[j])
        j += 1
    }
}

if i < a.count {
    // put remaining items into aUnique
    aUnique += a[i ..< a.count]
} else if j < b.count {
    // put remaining items into bUnique
    bUnique += b[j ..< b.count]
}

print(common)  // ["a"]
print(aUnique) // ["b", "c"]
print(bUnique) // ["d", "e"]


分析

  • 此算法每次通过循环将一项添加到数组之一.如果两个数组相对于彼此唯一,或者只有最后一个数组是公共的,它将最多循环a.count + b.count - 1次.
  • 如果两个数组相同,则仅循环a.count次.
  • 如果数组b的所有元素都大于数组a的元素,它将仅循环a.count次.如果数组a的所有元素都大于数组b的元素,它将仅循环b.count次.
  • This algorithm appends one item to one of the arrays each time through the loop. It will loop at most a.count + b.count - 1 times if both arrays are unique relative to each other, or only their last item is common.
  • If both arrays are identical, it will loop only a.count times.
  • If all elements of array b are greater than the elements of array a, it will loop only a.count times. If all elements of array a are greater than the elements of array b, it will loop only b.count times.

这篇关于快速-性能明智-比较两个数组并获得两者的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆