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
168
votes
5 answers

Why is the order in dictionaries and sets arbitrary?

I don't understand how looping over a dictionary or set in python is done by 'arbitrary' order. I mean, it's a programming language so everything in the language must be 100% determined, correct? Python must have some kind of algorithm that decides…
user1971598
163
votes
9 answers

How to convert a set to a list in python?

I am trying to convert a set to a list in Python 2.6. I'm using this syntax: first_list = [1,2,3,4] my_set=set(first_list) my_list = list(my_set) However, I get the following stack trace: Traceback (most recent call last): File "", line…
gath
  • 24,504
  • 36
  • 94
  • 124
157
votes
4 answers

Different types of thread-safe Sets in Java

There seems to be a lot of different implementations and ways to generate thread-safe Sets in Java. Some examples include 1) CopyOnWriteArraySet 2) Collections.synchronizedSet(Set set) 3) ConcurrentSkipListSet 4) Collections.newSetFromMap(new…
Ben
  • 2,430
  • 3
  • 22
  • 24
154
votes
5 answers

How do I add two sets?

a = {'a', 'b', 'c'} b = {'d', 'e', 'f'} How do I add the above two sets? I expect the result: c = {'a', 'b', 'c', 'd', 'e', 'f'}
GThamizh
  • 2,054
  • 3
  • 17
  • 19
153
votes
5 answers

How to check if a table contains an element in Lua?

Is there a method for checking if a table contains a value ? I have my own (naive) function, but I was wondering if something "official" exists for that ? Or something more efficient... function table.contains(table, element) for _, value in…
Wookai
  • 20,883
  • 16
  • 73
  • 86
152
votes
4 answers

How can I create a Set of Sets in Python?

I'm trying to make a set of sets in Python. I can't figure out how to do it. Starting with the empty set xx: xx = set([]) # Now we have some other set, for example elements = set([2,3,4]) xx.add(elements) but I get TypeError: unhashable type:…
Matt
  • 1,521
  • 2
  • 9
  • 3
151
votes
3 answers

Time complexity of python set operations?

What is the the time complexity of each of python's set operations in Big O notation? I am using Python's set type for an operation on a large number of items. I want to know how each operation's performance will be affected by the size of the set.…
Stephen Emslie
  • 10,539
  • 9
  • 32
  • 28
149
votes
7 answers

JavaScript Set vs. Array performance

It may be because Set is relatively new to JavaScript but I haven't been able to find an article, on Stack Overflow or anywhere else, that talks about the performance difference between the two in JavaScript. So, what is the difference, in terms of…
snowfrogdev
  • 5,963
  • 3
  • 31
  • 58
147
votes
4 answers

Difference between Iterator and Listiterator?

Iterator ite = Set.iterator(); Iterator ite = List.iterator(); ListIterator listite = List.listIterator(); We can use Iterator to traverse a Set or a List or a Map. But ListIterator can only be used to traverse a List, it can't traverse a Set.…
Siva
  • 3,297
  • 7
  • 29
  • 35
146
votes
21 answers

How to sort a HashSet?

For lists, we use the Collections.sort(List) method. What if we want to sort a HashSet?
Diana
  • 1,555
  • 4
  • 11
  • 17
143
votes
8 answers

Collection that allows only unique items in .NET?

Is there a collection in C# that will not let you add duplicate items to it? For example, with the silly class of public class Customer { public string FirstName { get; set; } public string LastName { get; set; } public string Address {…
Adam Rackis
  • 82,527
  • 56
  • 270
  • 393
143
votes
6 answers

random.choice from set? python

I'm working on an AI portion of a guessing game. I want the AI to select a random letter from this list. I'm doing it as a set so I can easily remove letters from the list as they are guessed in the game and are therefore no longer available to be…
jamyn
  • 1,683
  • 3
  • 15
  • 13
142
votes
5 answers

Use curly braces to initialize a Set in Python

I'm learning python, and I have a novice question about initializing sets. Through testing, I've discovered that a set can be initialized like so: my_set = {'foo', 'bar', 'baz'} Are there any disadvantages of doing it this way, as opposed to the…
fvrghl
  • 3,642
  • 5
  • 28
  • 36
136
votes
11 answers

Any implementation of Ordered Set in Java?

If anybody is familiar with Objective-C there is a collection called NSOrderedSet that acts as Set and its items can be accessed as an Array's ones. Is there anything like this in Java? I've heard there is a collection called LinkedHashMap, but I…
Uko
  • 13,134
  • 6
  • 58
  • 106
132
votes
4 answers

Is it possible to use getters/setters in interface definition?

At the moment, TypeScript does not allow use get/set methods(accessors) in interfaces. For example: interface I { get name():string; } class C implements I { get name():string { return null; } } furthermore, TypeScript…
Ivan Popov
  • 1,331
  • 2
  • 8
  • 5