如何获得斯威夫特2数组的共同元素列表 [英] How to get list of common elements of 2 array in swift

查看:136
本文介绍了如何获得斯威夫特2数组的共同元素列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数组:

fruitsArray = ["apple", "mango", "blueberry", "orange"]
vegArray = ["tomato", "potato", "mango", "blueberry"]

我怎样才能在这两个阵列这就给普通物品清单

How can I get the list of common items in those two array which gives

ouptput = ["mango", "blueberry"]

我不能使用如果包含(数组,字符串),因为我想比较2阵列。
请帮助

I cant use if contains(array, string) as I want to compare 2 arrays. Please help

推荐答案

您不需要一个集(如注释上面所提到的)。

您可以改为使用的通用的功能,一个类似于苹果在他们斯威夫特旅游使用,,从而避免铸造

You could instead use a generic function, similar to the one Apple use in their Swift Tour, and thus avoid casting:

func anyCommonElements <T, U where T: SequenceType, U: SequenceType, T.Generator.Element: Equatable, T.Generator.Element == U.Generator.Element> (lhs: T, rhs: U) -> Bool {
    for lhsItem in lhs {
        for rhsItem in rhs {
            if lhsItem == rhsItem {
                return true
            }
        }
    }
    return false
}

此功能可以采取任何两个数组(SequenceTypes),如果他们的任何元素都是相同返回true。

This function can take any two arrays (SequenceTypes) and if any of their elements are the same it returns true.

您可以简单地修改这个通用的功能打包字符串数组并返回来代替。

You could simply modify this generic function to package up an array of strings and return that instead.

例如这样的:

func arrayOfCommonElements <T, U where T: SequenceType, U: SequenceType, T.Generator.Element: Equatable, T.Generator.Element == U.Generator.Element> (lhs: T, rhs: U) -> [T.Generator.Element] {
    var returnArray:[T.Generator.Element] = []
    for lhsItem in lhs {
        for rhsItem in rhs {
            if lhsItem == rhsItem {
                returnArray.append(lhsItem)
            }
        }
    }
    return returnArray
}

使用方法是这样的:

var one = ["test2", "dog", "cat"]
var other = ["test2", "cat", "dog"]


var result = arrayOfCommonElements(one,other)

print(result) //prints [test2, dog, cat]

这里的好处是,该功能也可以与所有相同类型化阵列。所以后来如果你需要比较两个 [myCustomObject] 阵列,一旦他们都符合equatable,你所有的设置! (双关语意)

The added benefit here is that this function also works with all same typed arrays. So later if you need to compare two [myCustomObject] arrays, once they both conform to equatable, you're all set! (pun intended)

编辑:(有关的的共同要素),你可以做这样的事情。

(For non common elements) you could do something like this

func arrayOfNonCommonElements <T, U where T: SequenceType, U: SequenceType, T.Generator.Element: Equatable, T.Generator.Element == U.Generator.Element> (lhs: T, rhs: U) -> [T.Generator.Element] {

    var returnArray:[T.Generator.Element] = []
    var found = false

    for lhsItem in lhs {
        for rhsItem in rhs {
            if lhsItem == rhsItem {
                found = true
                break
            }
        }

        if (!found){
            returnArray.append(lhsItem)
        }

        found = false
    }
    for rhsItem in rhs {
        for lhsItem in lhs {
            if rhsItem == lhsItem {
                found = true
                break
            }
        }

        if (!found){
            returnArray.append(rhsItem)
        }

        found = false
    }
    return returnArray
}

这是实施虽然丑

这篇关于如何获得斯威夫特2数组的共同元素列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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