I just wanted to check whether there contains any duplicates in my array. I searched on Google and see some approaches:
我只是想检查数组中是否包含重复的内容。我在谷歌上搜索了一些方法:
- Double for-loop loops though the array and comparing each item
- 双循环循环遍历数组并比较每个项目
- Creating a dictionary that stores the number of occurrences of each item
- 创建一个字典来存储每个条目出现的次数
But these methods require a lot of loops and I'm kind of lazy to write a large amount of code just for this functionality. xD.
但是这些方法需要很多循环,我有点懒于为这个功能编写大量的代码。xD。
So I thought of this creative way:
所以我想到了这个创造性的方法:
let containsDuplicates = Set(array).count != array.count
However, is this method faster or slower than the other two? I'm not sure because it seems to create a set which I think needs to loop through the array. And I don't know whether accessing the count
also loops through the whole array.
然而,这个方法比另外两个更快还是更慢?我不确定,因为它似乎创建了一个集合,我认为需要对数组进行循环。我不知道访问计数是否也在整个数组中循环。
If I only have at most 50 items in the array, will this even matter?
如果数组中最多只有50个项目,那么这是否有关系?
2 个解决方案
#1
1
That's pretty much the best way. Dictionary storage is constant time, and each item only needs to be stored once, so it's an O(n)
solution, vs the O(n^2)
for the double for-loop approach. The space efficiency is lower, but that's usually not a concern these days.
这几乎是最好的方法。字典存储是恒定的时间,每一项只需要存储一次,所以这是一个O(n)的解决方案,对O(n ^ 2)双重循环的方法。空间效率较低,但这在现在已经不是问题了。
If you're doing this often, I would suggest using some kind of hybrid data structure, so that you're not constantly generating these Set
s from scratch.
如果您经常这样做,我建议您使用某种混合数据结构,这样您就不会经常从头开始生成这些集合。
#2
0
My first thought was to create a set and to see if the number of elements was different than the array, just like you. Apple will probably have optimized this conversion fairly well and doing it this way makes the code compact and easy to understand.
我的第一个想法是创建一个集合,看看元素的数量是否与数组不同,就像您一样。苹果可能已经很好地优化了这种转换,这样做可以使代码更紧凑,更容易理解。
You don't mention what the array is for and how it's used but could you just use a set instead of the array so that duplicates don't get added in the first place? Or if you need to keep track of how many but still need to know the unique values easily then create a new class that uses a dictionary for the storage. The key is what you are storing in the array but would be unique and the value would be the count.
你没有提到数组是做什么用的以及它是怎么用的但是你能不能用一个集合而不是数组这样重复的东西就不会被添加进来?或者,如果您需要跟踪有多少个但仍然需要轻松地知道惟一值,那么创建一个使用字典进行存储的新类。键是您在数组中存储的内容,但将是惟一的,值将是计数。
#1
1
That's pretty much the best way. Dictionary storage is constant time, and each item only needs to be stored once, so it's an O(n)
solution, vs the O(n^2)
for the double for-loop approach. The space efficiency is lower, but that's usually not a concern these days.
这几乎是最好的方法。字典存储是恒定的时间,每一项只需要存储一次,所以这是一个O(n)的解决方案,对O(n ^ 2)双重循环的方法。空间效率较低,但这在现在已经不是问题了。
If you're doing this often, I would suggest using some kind of hybrid data structure, so that you're not constantly generating these Set
s from scratch.
如果您经常这样做,我建议您使用某种混合数据结构,这样您就不会经常从头开始生成这些集合。
#2
0
My first thought was to create a set and to see if the number of elements was different than the array, just like you. Apple will probably have optimized this conversion fairly well and doing it this way makes the code compact and easy to understand.
我的第一个想法是创建一个集合,看看元素的数量是否与数组不同,就像您一样。苹果可能已经很好地优化了这种转换,这样做可以使代码更紧凑,更容易理解。
You don't mention what the array is for and how it's used but could you just use a set instead of the array so that duplicates don't get added in the first place? Or if you need to keep track of how many but still need to know the unique values easily then create a new class that uses a dictionary for the storage. The key is what you are storing in the array but would be unique and the value would be the count.
你没有提到数组是做什么用的以及它是怎么用的但是你能不能用一个集合而不是数组这样重复的东西就不会被添加进来?或者,如果您需要跟踪有多少个但仍然需要轻松地知道惟一值,那么创建一个使用字典进行存储的新类。键是您在数组中存储的内容,但将是惟一的,值将是计数。