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

10695 questions
1915
votes
103 answers

Get all unique values in a JavaScript array (remove duplicates)

I have an array of numbers that I need to make sure are unique. I found the code snippet below on the internet and it works great until the array has a zero in it. I found this other script here on Stack Overflow that looks almost exactly like it,…
Mottie
  • 74,638
  • 25
  • 119
  • 230
950
votes
30 answers

Get difference between two lists

I have two lists in Python, like these: temp1 = ['One', 'Two', 'Three', 'Four'] temp2 = ['One', 'Two'] I need to create a third list with items from the first list which aren't present in the second one. From the example I have to get temp3 =…
Max Frai
  • 52,556
  • 73
  • 182
  • 293
769
votes
19 answers

How to convert an Array to a Set in Java

I would like to convert an array to a Set in Java. There are some obvious ways of doing this (i.e. with a loop) but I would like something a bit neater, something like: java.util.Arrays.asList(Object[] a); Any ideas?
user130076
544
votes
14 answers

Does Python have an ordered set?

Python has an ordered dictionary. What about an ordered set?
Casebash
  • 100,511
  • 79
  • 236
  • 337
542
votes
15 answers

Convert Set to List without creating new List

I am using this code to convert a Set to a List: Map> mainMap = new HashMap<>(); for (int i=0; i < something.size(); i++) { Set set = getSet(...); //returns different result each time List listOfNames = new…
Muhammad Imran Tariq
  • 20,100
  • 42
  • 115
  • 186
519
votes
7 answers

Empty set literal?

[] = empty list () = empty tuple {} = empty dict Is there a similar notation for an empty set? Or do I have to write set()?
Johan Råde
  • 18,399
  • 19
  • 62
  • 103
518
votes
7 answers

C# Set collection?

Does anyone know if there is a good equivalent to Java's Set collection in C#? I know that you can somewhat mimic a set using a Dictionary or a HashTable by populating but ignoring the values, but that's not a very elegant way.
Omar Kooheji
  • 50,943
  • 65
  • 176
  • 234
506
votes
15 answers

How to retrieve an element from a set without removing it?

Suppose the following: >>> s = set([1, 2, 3]) How do I get a value (any value) out of s without doing s.pop()? I want to leave the item in the set until I am sure I can remove it - something I can only be sure of after an asynchronous call to…
Daren Thomas
  • 61,662
  • 38
  • 143
  • 192
449
votes
27 answers

What is the difference between Set and List?

What is the fundamental difference between the Set and List interfaces?
Johanna
  • 24,844
  • 39
  • 85
  • 114
440
votes
8 answers

Append values to a set in Python

I have a set like this: keep = set(generic_drugs_mapping[drug] for drug in drug_input) How do I add values [0,1,2,3,4,5,6,7,8,9,10] into this set?
Alex Gordon
  • 51,480
  • 273
  • 609
  • 976
380
votes
10 answers

How to check that an element is in a std::set?

How do you check that an element is in a set? Is there a simpler equivalent of the following code: myset.find(x) != myset.end()
fulmicoton
  • 13,826
  • 7
  • 50
  • 71
370
votes
22 answers

Getting an element from a Set

Why doesn't Set provide an operation to get an element that equals another element? Set set = ...; ... Foo foo = new Foo(1, 2, 3); Foo bar = set.get(foo); // get the Foo element from the Set that equals foo I can ask whether the Set contains…
foobar
  • 4,338
  • 3
  • 14
  • 10
339
votes
9 answers

How to get the first element of the List or Set?

I'd like to know if I can get the first element of a list or set. Which method to use?
user496949
  • 75,601
  • 138
  • 297
  • 413
321
votes
7 answers

Best way to find the intersection of multiple sets?

I have a list of sets: setlist = [s1,s2,s3...] I want s1 ∩ s2 ∩ s3 ... I can write a function to do it by performing a series of pairwise s1.intersection(s2), etc. Is there a recommended, better, or built-in way?
user116293
  • 4,864
  • 4
  • 21
  • 17
315
votes
12 answers

In Python, when to use a Dictionary, List or Set?

When should I use a dictionary, list or set? Are there scenarios that are more suited for each data type?
Blankman
  • 236,778
  • 296
  • 715
  • 1,125
1
2 3
99 100