Can a collection have repeating elements?

I was asked a question that is a bit ambiguous for my term paper.

The array of strings is regarded as a set, ie unordered. 

I'm not sure if duplicates should be removed from this array?

I tried searching, but one place will tell me something else. Any help would be appreciated.

+9
source share
4 answers

Let A = {1,2,2,3,4,5,6,7, ...} and B = {1,2,3,4,5,6,7, ...}, then any element from A is in B, and any element from B is in ==> A contains B and B contains A ==> A = B. So, of course, sets can have duplicate elements, it's just that one with duplicate elements will be exactly the same as one that has no repeating elements.

+7
source

From Wikipedia to Set (Mathematics)

A set is a set of clearly defined and different objects.

Perhaps the confusion stems from the fact that the set does not depend on how its elements are displayed. A set remains unchanged if its elements are supposedly repeated or reordered.

Thus, the programming languages ​​that I know will not put an element into a set if the element already belongs to it, or they would replace it if it already exists, but would never allow duplication.

Programming Language Examples

Let me offer some examples in different programming languages.

In python

A collection in Python is defined as an "unordered collection of unique elements." And if you declare a set as a = {1,2,2,3,4} , it will add only 2 to the set.

If you execute print(a) , the output will be {1,2,3,4} .

Haskell

In Haskell, the operation of inserting sets is defined as: "[...] if the set already contains an element equal to the given value, it is replaced by the new value."

Thus, if you do this: let a = fromList([1,2,2,3,4]) , if you type a in the main output, it will display [1,2,3,4] .

Java

Java suites define: "a collection that does not contain duplicate elements." Its adding operation is defined as: "adds the specified element to this set, if it is not already present [...] If this set already contains the element, the call leaves the parameter unchanged."

 Set<Integer> myInts = new HashSet<>(asList(1,2,2,3,4)); System.out.println(myInts); 

This code, as in other examples, will output [1,2,3,4] .

+27
source

A set cannot have duplicate elements by its simple definition. The correct structure to duplicate elements, Multiset or Bag :

In mathematics, a multiset (or bag) is a generalization of the concept of a set, which, unlike a set, allows multiple instances of many sets. For example, {a, a, b} and {a, b} are different multisets, although they are the same. However, the order does not matter, so {a, a, b} and {a, b, a} are the same multiset.

A very common and useful example of Multiset in programming is the collection of object values:

 values({a: 1, b: 1}) //=> Multiset(1,1) 

The values ​​here are unordered, but cannot be reduced to Set(1) , which, for example, break iteration over the values ​​of an object.

Further, citing from a related Wikipedia article (see links there):

Multilinks have become an important tool in databases. [18] [19] [20] For example, multi-networks are often used to implement relationships in database systems. Multisets also play an important role in computer science.

+3
source

"Sets are iterations that do not contain duplicate elements." https://docs.scala-lang.org/overviews/collections/sets.html

0
source

All Articles