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
222
votes
12 answers

How to JSON serialize sets?

I have a Python set that contains objects with __hash__ and __eq__ methods in order to make certain no duplicates are included in the collection. I need to json encode this result set, but passing even an empty set to the json.dumps method raises a…
DeaconDesperado
  • 9,977
  • 9
  • 47
  • 77
217
votes
9 answers

How to join two sets in one line without using "|"

Assume that S and T are assigned sets. Without using the join operator |, how can I find the union of the two sets? This, for example, finds the intersection: S = {1, 2, 3, 4} T = {3, 4, 5, 6} S_intersect_T = { i for i in S if i in T } So how can…
fandyst
  • 2,740
  • 2
  • 14
  • 15
216
votes
34 answers

Picking a random element from a set

How do I pick a random element from a set? I'm particularly interested in picking a random element from a HashSet or a LinkedHashSet, in Java. Solutions for other languages are also welcome.
Clue Less
  • 3,147
  • 2
  • 20
  • 10
214
votes
16 answers

Converting a list to a set changes element order

Recently I noticed that when I am converting a list to set the order of elements is changed and is sorted by character. Consider this example: x=[1,2,20,6,210] print(x) # [1, 2, 20, 6, 210] # the order is same as initial order set(x) # set([1, 2,…
d.putto
  • 7,185
  • 11
  • 39
  • 45
212
votes
6 answers

Most concise way to convert a Set to a List

For example, I am currently doing this: Set setOfTopicAuthors = .... List list = Arrays.asList( setOfTopicAuthors.toArray( new String[0] ) ); Can you beat this ?
Jacques René Mesrine
  • 46,127
  • 27
  • 66
  • 104
209
votes
11 answers

How to add an array of values to a Set

The old school way of adding all values of an array into the Set is: // for the sake of this example imagine this set was created somewhere else // and I cannot construct a new one out of an array let mySet = new Set() for(let item of array) { …
Fuzzyma
  • 7,619
  • 6
  • 28
  • 60
206
votes
18 answers

Set value of hidden field in a form using jQuery's ".val()" doesn't work

I've been trying to set the value of a hidden field in a form using jQuery, but without success. Here is a sample code that explains the problem. If I keep the input type to "text", it works without any trouble. But, changing the input type to…
texens
  • 3,717
  • 5
  • 23
  • 23
204
votes
18 answers

comparing ECMA6 sets for equality

How do you compare two javascript sets? I tried using == and === but both return false. a = new Set([1,2,3]); b = new Set([1,3,2]); a == b; //=> false a === b; //=> false These two sets are equivalent, because by definition, sets do not have order…
williamcodes
  • 6,317
  • 8
  • 32
  • 55
200
votes
32 answers

How to get all subsets of a set? (powerset)

Given a set {0, 1, 2, 3} How can I produce the subsets: [set(), {0}, {1}, {2}, {3}, {0, 1}, {0, 2}, {0, 3}, {1, 2}, {1, 3}, {2, 3}, {0, 1, 2}, {0, 1, 3}, {0, 2, 3}, {1, 2, 3}, {0, 1, 2, 3}]
patrick
  • 16,091
  • 29
  • 100
  • 164
198
votes
22 answers

How to set background color of a View

I'm trying to set the background color of a View (in this case a Button). I use this code: // set the background to green v.setBackgroundColor(0x0000FF00 ); v.invalidate(); It causes the Button to disappear from the screen. What am I doing wrong,…
Peter vdL
  • 4,953
  • 10
  • 40
  • 60
193
votes
6 answers

How do I add the contents of an iterable to a set?

What is the "one [...] obvious way" to add all items of an iterable to an existing set?
Ian Mackinnon
  • 13,381
  • 13
  • 51
  • 67
191
votes
8 answers

Deleting elements from std::set while iterating

I need to go through a set and remove elements that meet a predefined criteria. This is the test code I wrote: #include #include void printElement(int value) { std::cout << value << " "; } int main() { int initNum[] = {…
pedromanoel
  • 3,232
  • 2
  • 24
  • 23
182
votes
7 answers

Python set to list

How can I convert a set to a list in Python? Using a = set(["Blah", "Hello"]) a = list(a) doesn't work. It gives me: TypeError: 'set' object is not callable
user825286
177
votes
3 answers

Best way to convert list to comma separated string in java

I have Set result & would like to convert it to comma separated string. My approach would be as shown below, but looking for other opinion as well. List slist = new ArrayList (result); StringBuilder rString = new…
Mad-D
  • 4,479
  • 18
  • 52
  • 93
175
votes
20 answers

Is there hash code function accepting any object type?

Basically, I'm trying to create an object of unique objects, a set. I had the brilliant idea of just using a JavaScript object with objects for the property names. Such as, set[obj] = true; This works, up to a point. It works great with string and…
Boog
  • 3,744
  • 4
  • 28
  • 25