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

Finding the minimum set of properties that describe a referent in a set of entities

I was wondering if someone could help me get pointers to solve this problem. A link to algorithms would be great, but pointers to papers/info is also good. The problem is as follows. Suppose I have a set E of entities E={car1, car2, bicycle} and a…
Dervin Thunk
  • 19,515
  • 28
  • 127
  • 217
5
votes
3 answers

Concurrent set with weak references and identity hash

I wanted a concurrent set with weak references to elements. I thought of doing this using Guava's MapMaker: Set concurrentSet = Collections.newSetFromMap( new MapMaker().weakKeys().makeMap()); Guava will automatically…
r.v
  • 4,697
  • 6
  • 35
  • 57
5
votes
3 answers

How is std::set slower than std::map?

I was trying to solve this problem from acm.timus.ru which basically wants me to output the number of different substrings of a given string (max length 5000). The solutions I am about to present are desperately inefficient and doomed for Time…
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
5
votes
2 answers

Finding numbers from a to b not divisible by x to y

This is a problem I've been pondering for quite some time. What is the fastest way to find all numbers from a to b that are not divisible by any number from x to y? Consider this: I want to find all the numbers from 1 to 10 that are not divisible by…
JohnWO
  • 203
  • 3
  • 13
5
votes
4 answers

Convert type in setter method, possible? When yes how?

guess I wanted to gernerate a commandline with flags and so on. Flags are of type bool but the commandline is a string like " /activeFlag". Is there a way to program a setter in C# which takes a bool but the getter returns a string? like private…
Ephraim
  • 767
  • 1
  • 8
  • 15
5
votes
2 answers

Set of maximal independent subsets via a c# generator

I want to find all subsets of a given set that are mutually exclusive and contain as many elements of the superset as possible. Where the user defines a meaning for exclusiveness: bool exclusion(T a, T b) where at least exclusion(a, b) ==…
Maxwell
  • 303
  • 1
  • 7
5
votes
5 answers

Test if set is a subset, considering the number (multiplicity) of each element in the set

I know I can test if set1 is a subset of set2 with: {'a','b','c'} <= {'a','b','c','d','e'} # True But the following is also True: {'a','a','b','c'} <= {'a','b','c','d','e'} # True How do I have it consider the number of times an element in the set…
Joe Flip
  • 1,076
  • 4
  • 21
  • 37
5
votes
2 answers

Different exceptions for pop from empty sets and lists?

Why do empty sets and lists raise different exceptions when you call .pop()? >>> l = [] >>> l.pop() Traceback (most recent call last): File "", line 1, in l.pop() IndexError: pop from empty list >>> l = set() >>>…
Joschua
  • 5,816
  • 5
  • 33
  • 44
5
votes
5 answers

Using HashSet to create an integer set

I want to create an class that represents an integer set using a HashSet. I want it to keep track of which values are included in the set using that internal container. I've done this so far: class SetInteger { HashSet intTest= new…
user2057693
  • 285
  • 3
  • 7
  • 13
5
votes
4 answers

Take and remove elements from a Ruby Set in one operation

I have a Set of elements from which I want to take and remove the first few elements a bunch of times. Is there a shorter way (so one operation instead of two) to do that than this: require 'set' s = Set[1, 2, 3, 4] # => #
Confusion
  • 16,256
  • 8
  • 46
  • 71
5
votes
9 answers

Why doesn't .Net have a Set data structure?

One of my biggest issues dealing with a move from Java to .Net is the fact that there isn't a Set interface in .Net. I know there are libraries I could go and download but what is the reason for not having it built in? There are Maps (Dictionary)…
Joe Phillips
  • 49,743
  • 32
  • 103
  • 159
5
votes
2 answers

How to create dictionary from two lists without losing duplicate values?

I have two lists: pin_list = ['in0', 'in1', 'in2', 'y'] delvt_list = ['0.399', '0.1995', '0.1995', '0.399'] I use the code: temp = dict(zip(delvt_list,pin_list)) but I get the following: temp = {'0.1995': 'in2', '0.399': 'y'} What Python code do I…
Jon A.
  • 81
  • 1
  • 7
5
votes
2 answers

Is there any code to set wallpaper without cropping and zooming in android?

i am creating a gallery app , my first app and this is my code Bitmap bmd = BitmapFactory.decodeStream(is); try{ getApplicationContext().setWallpaper(bmd); }catch(IOException e){ e.printStackTrace(); } The above…
Kalpana
  • 59
  • 1
  • 3
5
votes
8 answers

What is a "set" in C++? When are they useful?

I'm having a hard time conceptualizing c++ sets, actually sets in general. What are they? How are they useful?
Monte Hurd
  • 4,349
  • 5
  • 30
  • 35
5
votes
1 answer

Object.defineProperty get/set closure

Ok, I try to create new object this way: var src = {a:'a', b:'b', c:'c'}; var out = {}; for(var prop in src){ Object.defineProperty(out, prop,{ get: function(){ return src[prop]; }, set: function(val){ …
3y3
  • 802
  • 1
  • 6
  • 18
1 2 3
99
100