Questions tagged [set]

A set is a collection in which no element is repeated, which may be able to enumerate its elements according to an ordering criterion (an "ordered set") or retain no order (an "unordered set").

A set is a collection in which no element is repeated. It is often implemented by hashing the objects as they are added to the set, and comparing against those hashes for operations on the set.

In the C++ standard library in particular, the std::set is able to enumerate its elements according to a specific strict weak ordering criterion set on container construction. To achieve this, it is typically implemented by a binary tree. By contrast, the std::unordered_set stores unique elements in no particular order, and allows for fast retrieval of individual elements based on their value.

In Python, there are currently two built-in set types, set and frozenset. set is mutable, i.e. the contents can be changed and it has no hash value and cannot be used as either a dictionary key or as an element of another set. The frozenset type is immutable and hashable.

Common operations on sets:

  • add
  • remove
  • find (check membership)
  • union, intersection, difference

Resources

12019 questions
5
votes
3 answers

Algorithm to produce all partitions of a list in order

I've need for a particular form of 'set' partitioning that is escaping me, as it's not quite partitioning. Or rather, it's the subset of all partitions for a particular list that maintain the original order. I have a list of n elements [a,b,c,...,n]…
Rachek
  • 120
  • 1
  • 6
5
votes
1 answer

How can I point to a member of a std::set in such a way that I can tell if the element has been removed?

An iterator into a std::set becomes invalidated if the item it's pointing to is erased. (It does not get invalidated if the set is modified in any other way, which is nice.) However, there is no way to detect whether an iterator has been invalidated…
N. Virgo
  • 7,970
  • 11
  • 44
  • 65
5
votes
1 answer

Finding all possible sums of a set

I am currently looking for ideas on how to find all possible sums of a set of numbers with these rules. I have these numbers to work with and I want to find all possible sums so that you can only use a single number at max 4 times and each time you…
5
votes
2 answers

Why does collections.MutableSet not bestow an update method?

When implementing a class that works like a set, one can inherit from collections.MutableSet, which will bestow the new class with several mixin methods, if you implement the methods they require. (Said otherwise, some of the methods of a set can be…
Thanatos
  • 42,585
  • 14
  • 91
  • 146
5
votes
5 answers

Set collection preserving insertion order

I need a collection that behaves as Set and preserves order of element insertion. Is there one or I'll have to implement it myself? What would the best implementation be?
Konstantin Spirin
  • 20,609
  • 15
  • 72
  • 90
5
votes
2 answers

Scheme: Detecting duplicate elements in a list

Does R6RS or Chez Scheme v7.9.4 have a library function to check if a list contains duplicate elements? Alternatively, do either have any built in functionality for sets (which dis-allow duplicate elements)? So far, I've only been able to find an…
Kyle Krull
  • 1,618
  • 2
  • 18
  • 25
5
votes
3 answers

Finding values common to all elements

I have a dataset with two columns as follows; Prod Value A 1 A 2 A 3 B 2 B 4 C 1 C 2 So I want all the rows where value is common for Prod, so in the example above only the following would be returned. A 2 B 2 C 2…
Dub
  • 61
  • 2
5
votes
1 answer

passing more data to std:set Comparison class

I have an std::set with the Compare class which requires additional parameter to compare keys. This variable parameter is determined in run-time and I pack it inside the set's keys just to make it accessible to Compare. However, the parameter…
jackhab
  • 17,128
  • 37
  • 99
  • 136
5
votes
1 answer

Is this legal to avoid set from creating actual copies of Comparator object

In such a code: Comparator comp(3); set s1(comp); set s2(comp); set s3(comp); set s4(comp); the actual instance of the Comparator (namely comp) is copied at each…
Issam T.
  • 1,677
  • 1
  • 14
  • 32
5
votes
1 answer

Transitive set merging

Is there a well-known algorithm, that given a collection of sets, would merge together every two sets that have at least one common element? So for example, given the following input: A B C B D A E F G H I J K F L M N E O It would produce: A B C D…
sagi
  • 5,619
  • 1
  • 30
  • 31
5
votes
2 answers

Merge pairs that share one element in common

I have the following data structure: set< pair > data; data = {<1, 1> ; <3, 2> ; <5, 5> ; <5, 4> ; <4, 2>, <1, 8>, <9, 9> } I would like to merge pairs which contain at least one common element and store the result in a vector of…
John
  • 53
  • 2
5
votes
2 answers

How does set::emplace handle objects that are already in a set?

I have a set of objects, and I want to use emplace to add objects to the set. If an equivalent object does not already exist in the set, set::emplace creates an object and puts it in the set. If the set already has an equivalent object, set::emplace…
Qaz
  • 1,556
  • 2
  • 20
  • 34
5
votes
6 answers

Accessing elements from map>

I am working with a data structure that looks like this: map> data; By now, I had no problems with working with the map by using foreach cycle, however, now I need to print out the data from the map like this: KEY: elem1,…
Lea
  • 211
  • 1
  • 2
  • 14
5
votes
4 answers

Why there are no overloads of find/lower_bound for std::map and no overload of sort for std::list?

I know that you should never use std::find(some_map.begin(), some_map.end()) or std::lower_bound, because it will take linear time instead of logarithmic provided by some_map.lower_bound. Similar thing happens with std::list: there is…
yeputons
  • 8,478
  • 34
  • 67
5
votes
2 answers

Why will (seq #{3 1 22 44}) comes out (1 3 44 22) in clojure?

How does it work? (seq #{3 1 22 44}) And why the order will be like (1 3 44 22)
procr
  • 583
  • 1
  • 5
  • 14