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
323
votes
8 answers

How to Iterate over a Set/HashSet without an Iterator?

How can I iterate over a Set/HashSet without the following? Iterator iter = set.iterator(); while (iter.hasNext()) { System.out.println(iter.next()); }
user1621988
  • 4,215
  • 8
  • 27
  • 31
303
votes
4 answers

golang why don't we have a set datastructure

I'm trying to solve "The go programming lanaguage" exercise #1.4 which requires me to have a set. I can create a set type but why doesn't the language come with one ? go, having come from google, where guava also originated, why didn't the language…
anjanb
  • 12,999
  • 18
  • 77
  • 106
303
votes
6 answers

JavaScript Array to Set

MDN references JavaScript's Set collection abstraction. I've got an array of objects that I'd like to convert to a set so that I am able to remove (.delete()) various elements by name: var array = [ {name: "malcom", dogType: "four-legged"}, …
Thomas
  • 6,291
  • 6
  • 40
  • 69
299
votes
15 answers

Simplest way to merge ES6 Maps/Sets?

Is there a simple way to merge ES6 Maps together (like Object.assign)? And while we're at it, what about ES6 Sets (like Array.concat)?
jameslk
  • 4,298
  • 4
  • 21
  • 19
288
votes
2 answers

How to calculate the intersection of two sets?

Possible Duplicate: Efficiently finding the intersection of a variable number of sets of strings Say, have two Hashset, how to calculate the intersection of them? Set s1 = new HashSet(); Set s2 = new…
user496949
  • 83,087
  • 147
  • 309
  • 426
271
votes
11 answers

How to customize object equality for JavaScript Set

New ES 6 (Harmony) introduces new Set object. Identity algorithm used by Set is similar to === operator and so not much suitable for comparing objects: var set = new Set(); set.add({a:1}); set.add({a:1}); console.log([...set.values()]); // Array […
czerny
  • 15,090
  • 14
  • 68
  • 96
269
votes
8 answers

How to construct a set out of list items in python?

I have a list of filenames in python and I would want to construct a set out of all the filenames. filelist=[] for filename in filelist: set(filename) This does not seem to work. How can do this?
securecoding
  • 2,763
  • 2
  • 15
  • 14
268
votes
6 answers

How to "perfectly" override a dict?

How can I make as "perfect" a subclass of dict as possible? The end goal is to have a simple dict in which the keys are lowercase. It would seem that there should be some tiny set of primitives I can override to make this work, but according to all…
Paul Biggar
  • 27,579
  • 21
  • 99
  • 152
256
votes
11 answers

Python Sets vs Lists

In Python, which data structure is more efficient/speedy? Assuming that order is not important to me and I would be checking for duplicates anyway, is a Python set slower than a Python list?
Mantas Vidutis
  • 16,376
  • 20
  • 76
  • 92
252
votes
6 answers

Swift Set to Array

An NSSet can be converted to Array using set.allObjects() but there is no such method in the new Set (introduced with Swift 1.2). It can still be done by converting Swift Set to NSSet and use the allObjects() method but that is not optimal.
Fried Rice
  • 3,295
  • 3
  • 18
  • 27
251
votes
5 answers

How to map/reduce/filter a Set in JavaScript?

Is there any way to map/reduce/filter/etc a Set in JavaScript or will I have to write my own? Here's some sensible Set.prototype extensions Set.prototype.map = function map(f) { var newSet = new Set(); for (var v of this.values())…
Mulan
  • 129,518
  • 31
  • 228
  • 259
247
votes
19 answers

Why doesn't java.util.Set have get(int index)?

I'm sure there's a good reason, but could someone please explain why the java.util.Set interface lacks get(int Index), or any similar get() method? It seems that sets are great for putting things into, but I can't find an elegant way of retrieving a…
Marty Pitt
  • 28,822
  • 36
  • 122
  • 195
245
votes
11 answers

Getting the difference between two sets

So if I have two sets: Set test1 = new HashSet(); test1.add(1); test1.add(2); test1.add(3); Set test2 = new HashSet(); test2.add(1); test2.add(2); test2.add(3); test2.add(4); test2.add(5); Is there a way to…
David Tunnell
  • 7,252
  • 20
  • 66
  • 124
240
votes
14 answers

Java Set retain order?

Does a Java Set retain order? A method is returning a Set to me and supposedly the data is ordered but iterating over the Set, the data is unordered. Is there a better way to manage this? Does the method need to be changed to return something…
user840930
  • 5,214
  • 21
  • 65
  • 94
228
votes
6 answers

How is set() implemented?

I've seen people say that set objects in python have O(1) membership-checking. How are they implemented internally to allow this? What sort of data structure does it use? What other implications does that implementation have? Every answer here was…
Daenyth
  • 35,856
  • 13
  • 85
  • 124