ios swift中数组的唯一值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27624331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 04:06:47  来源:igfitidea点击:

Unique values of array in swift

iosswift

提问by user3806505

I'm building an iOS app with swift and i need to get all unique values of array of strings.

我正在快速构建一个 iOS 应用程序,我需要获取字符串数组的所有唯一值。

I've been reading the apple developer docs but it doesn't seem to have a function for it.

我一直在阅读苹果开发人员文档,但它似乎没有相应的功能。

Can someone give me an hint?

有人可以给我一个提示吗?

回答by Logan

One way is to use a set:

一种方法是使用集合:

let array = ["one", "one", "two", "two", "three", "three"]
let unique = Array(Set(array))
// ["one", "two", "three"]

You could also create an extension that filters through the array more explicitly:

您还可以创建一个扩展来更明确地过滤数组:

extension Array where Element : Equatable {
    var unique: [Element] {
        var uniqueValues: [Element] = []
        forEach { item in
            if !uniqueValues.contains(item) {
                uniqueValues += [item]
            }
        }
        return uniqueValues
    }
}

NOTE

笔记

The unique array will be in an unspecified order, and you may need to sort it. Sometimes it's better to just do it yourself by enumerating, you could write an extension.

唯一数组将按未指定的顺序排列,您可能需要对其进行排序。有时最好通过枚举自己做,你可以写一个扩展。

It might be good to make an extension (Swift 2):

进行扩展(Swift 2)可能会很好:

extension Array where Element : Hashable {
    var unique: [Element] {
        return Array(Set(self))
    }
}

There are probably more optimized ways to do what you want, but this way is quick and easy.

可能有更优化的方法来做你想做的事,但这种方法既快捷又简单。

回答by Airspeed Velocity

There isn't a function to do this in the Swift standard library, but you could write one:

Swift 标准库中没有执行此操作的函数,但您可以编写一个:

extension Sequence where Iterator.Element: Hashable {
    func unique() -> [Iterator.Element] {
        var seen: [Iterator.Element: Bool] = [:]
        return self.filter { seen.updateValue(true, forKey: 
func distinct<S: SequenceType, E: Equatable where E==S.Generator.Element>(source: S) -> [E]
{
    var unique = [E]()

    for item in source
    {
        if !contains(unique, item)
        {
            unique.append(item)
        }
    }
    return unique
}
) == nil } } } let a = ["four","one", "two", "one", "three","four", "four"] a.unique // ["four", "one", "two", "three"]

This has the downside of requiring the contents of the sequence to be hashable, not just equatable, but then again most equatable things are, including strings.

这有一个缺点,即要求序列的内容是可散列的,不仅仅是可等价的,而且大多数可等价的东西也是,包括字符串。

It also preserves the original ordering unlike, say, putting the contents in a dictionary or set and then getting them back out again.

它还保留了原始顺序,而不是将内容放入字典或集合中,然后再将它们取出。

回答by Ben Kane

I don't know of a built in way. This generic function would do it:

我不知道内置的方式。这个通用函数可以做到:

##代码##

The downside here is that this solution runs in O(n2).

这里的缺点是这个解决方案在 O(n 2) 中运行。

回答by qwerty_so

Use a dictionary like var unique = [<yourtype>:Bool]()and fill in the values like unique[<array value>] = truein a loop. Now unique.keyshas what you need.

使用类似的字典var unique = [<yourtype>:Bool]()并像unique[<array value>] = true循环一样填充值。现在unique.keys有你需要的东西。