我被问到一个对我的类(class)作业来说有点模棱两可的问题。
The array of strings is regarded as a set, i.e. unordered.
我不确定是否需要从此数组中删除重复项?
我试过谷歌搜索,但一个地方会告诉我与下一个地方不同的东西。任何帮助,将不胜感激。
最佳答案
一个集合的定义不能有重复的元素。允许重复元素的正确结构是Multiset or Bag :
In mathematics, a multiset (or bag) is a generalization of the concept of a set that, unlike a set, allows multiple instances of the multiset's elements. For example, {a, a, b} and {a, b} are different multisets although they are the same set. However, order does not matter, so {a, a, b} and {a, b, a} are the same multiset.
编程中一个非常常见且有用的 Multiset 示例是对象值的集合:
values({a: 1, b: 1}) //=> Multiset(1,1)
这里的值是无序的,但不能减少到
Set(1)
那会例如打破对象值的迭代。此外,引用链接的维基百科文章(参见那里的引用资料):
Multisets have become an important tool in databases.[18][19][20] For instance, multisets are often used to implement relations in database systems. Multisets also play an important role in computer science.
关于set - 一个集合可以有重复的元素吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10011475/