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
131
votes
7 answers

How to set a Javascript object values dynamically?

It's difficult to explain the case by words, let me give an example: var myObj = { 'name': 'Umut', 'age' : 34 }; var prop = 'name'; var value = 'Onur'; myObj[name] = value; // This does not work eval('myObj.' + name) = value; //Bad…
Umut KIRGÖZ
  • 2,105
  • 3
  • 22
  • 29
129
votes
4 answers

How can I sort an ES6 `Set`?

new Set(['b', 'a', 'c']).sort() throws TypeError: set.sort is not a function. How can I sort a Set to ensure a particular iteration order?
ericsoco
  • 24,913
  • 29
  • 97
  • 127
128
votes
9 answers

What is the fastest way to compare two sets in Java?

I am trying to optimize a piece of code which compares elements of list. Eg. public void compare(Set firstSet, Set secondSet){ for(Record firstRecord : firstSet){ for(Record secondRecord : secondSet){ //…
Shekhar
  • 5,771
  • 10
  • 42
  • 48
122
votes
5 answers

Why can a Python dict have multiple keys with the same hash?

I am trying to understand the Python hash function under the hood. I created a custom class where all instances return the same hash value. class C: def __hash__(self): return 42 I just assumed that only one instance of the above class…
Praveen Gollakota
  • 37,112
  • 11
  • 62
  • 61
120
votes
24 answers

How can I simulate an array variable in MySQL?

It appears that MySQL doesn't have array variables. What should I use instead? There seem to be two alternatives suggested: A set-type scalar and temporary tables. The question I linked to suggests the former. But is it good practice to use these…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
119
votes
5 answers

How to iterate std::set?

I have this code: std::set::iterator it; for (it = SERVER_IPS.begin(); it != SERVER_IPS.end(); ++it) { u_long f = it; // error here } There is no ->first value. How I can obtain the value?
Roman
  • 1,377
  • 2
  • 11
  • 12
116
votes
3 answers

Set difference versus set subtraction

What distinguishes - and .difference() on sets? Obviously the syntax is not the same. One is a binary operator, and the other is an instance method. What else? s1 = set([1,2,3]) s2 = set([3,4,5]) >>> s1 - s2 set([1, 2]) >>>…
David542
  • 104,438
  • 178
  • 489
  • 842
116
votes
5 answers

How to store Query Result in variable using mysql

SET @v1 := SELECT COUNT(*) FROM user_rating; SELECT @v1 When I execute this query with set variable this error is shown. Error Code : 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the…
Query Master
  • 6,989
  • 5
  • 35
  • 58
115
votes
12 answers

Is there a better way to combine two string sets in java?

I need to combine two string sets while filtering out redundant information, this is the solution I came up with, is there a better way that anyone can suggest? Perhaps something built in that I overlooked? Didn't have any luck with…
FooBar
  • 1,663
  • 2
  • 12
  • 19
115
votes
8 answers

What makes sets faster than lists?

The python wiki says: "Membership testing with sets and dictionaries is much faster, O(1), than searching sequences, O(n). When testing "a in b", b should be a set or dictionary instead of a list or tuple." I've been using sets in place of lists…
user671110
115
votes
6 answers

How can I convert a Set to an Array in TypeScript

How can I convert a Set (eg, {2,4,6}) to an Array [2, 4, 6] in TypeScript without writing a loop explicitly ? I have tried those following ways, all of them work in JavaScript but none of them work on TypeScript [...set] // ERR: "Type 'Set<{}>' is…
thanhpk
  • 3,900
  • 4
  • 29
  • 36
114
votes
7 answers

android set custom font to a paint

I want to draw a text to a paint. How to draw it with a custom font (ex Helvetica ) and bold also? I would preffer to use a system font and not create it from assets. Thanks.
Buda Gavril
  • 21,409
  • 40
  • 127
  • 196
113
votes
6 answers

Why is '+' not understood by Python sets?

I would like to know why this is valid: set(range(10)) - set(range(5)) but this is not valid: set(range(10)) + set(range(5)) Is it because '+' could mean both intersection and union?
badzil
  • 3,440
  • 4
  • 19
  • 27
111
votes
8 answers

How to extract the member from single-member set in python?

I recently encountered a scenario in which if a set only contained a single element, I wanted to do something with that element. To get the element, I settled on this approach: element = list(myset)[0] But this isn't very satisfying, as it creates…
recursive
  • 83,943
  • 34
  • 151
  • 241
110
votes
4 answers

Reduce array to set in Swift

I am trying to reduce an array of objects to a set in Swift and this is my code: objects.reduce(Set()) { $0.insert($1.URL) } However, I get an error: Type of expression is ambiguous without more context. I do not understand what the…
Banana
  • 4,010
  • 9
  • 33
  • 49