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 subsets that can be completed to tuples without duplicates

We have a collection of sets A_1,..,A_n. The goal is to find new sets for each of the old sets. newA_i = {a_i in A_i such that there exist (a_1,..,a_n) in (A1,..,An) with no a_k = a_j for all k and j} So in words this says that we remove all the…
Jules
  • 6,318
  • 2
  • 29
  • 40
5
votes
6 answers

java: how do I create an array of tuples

how can I create an array of tuples in jsp (java) like (a:1, b:2) (c:3, d:4) ... ...
kkkkk
  • 51
  • 1
  • 1
  • 2
5
votes
3 answers

What's the complexity of map/set :: insert if one has provided a correct iterator hint?

Is it O(1) or O(logN) but with a smaller coefficient? If this is unspecified, I'd at least like to know the answer based on a reasonable supposition that the map/set is implemented using a red-black or AVL tree. The general algorithm to insert an…
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
5
votes
1 answer

How can the symmetric difference between two lists of two-element sublists be obtained?

I have two lists each of which contain two-element lists. a = [['Adolf', '10'], ['Hermann', '20'], ['Heinrich', '30'], ['Heinrich', '15']] b = [['Rudolf', '40'], ['Adolf', '50']] I want to get the 'symmetric difference' of the two lists, based on…
d3pd
  • 7,935
  • 24
  • 76
  • 127
5
votes
2 answers

Any existing .Net Ordered Set?

I'm looking for .Net class, which will basically: Ensure that items are unique in it(like an HashSet) Ensure that when we enumerate, we get items in the same order than we inserted them(Like a List) Is there an existing .Net class that does…
J4N
  • 19,480
  • 39
  • 187
  • 340
5
votes
4 answers

How to set protractor(v1.4.0) baseUrl using protractor API instead of configuration file?

i use protractor v1.4.0 and I want to set protractor baseUrl from command line so i can't use baseUrl: 'http://localhost:8000/', config option in protractor configuration file. I want to define default value for base url with "params" option in…
Suren Aznauryan
  • 206
  • 3
  • 10
5
votes
3 answers

Why don't I get an error when adding a duplicate item to a Set?

I added the same integer twice to a Set, but it will not give any error despite Sets not allowing duplicates. Why is this? Set set = new HashSet(); set.add(1); set.add(1);
Sopan K Kokre
  • 51
  • 1
  • 4
5
votes
2 answers

How is 1 greater than 4?

The NavigableSet.lower(E) Javadoc says it returns the greatest element in this set strictly less than the given element, or null if there is no such element. Why is 1 the output here? Shouldn't it be 4? NavigableSet original = new…
Leo
  • 5,017
  • 6
  • 32
  • 55
5
votes
6 answers

Problem in java.util.Set.addAll() method

I have a java.util.Set cities and I need to add cities to this set in 2 ways: By adding individual city (with the help of cities.add(city) method call) By adding another set of cities to this set (with the help of…
Amit
  • 33,847
  • 91
  • 226
  • 299
5
votes
2 answers

CSS set width to parentNode's width

Is there a way in CSS to set the width of any element to equal the width of its parentNode? I tried 'inherit' - sounds logic - but it didn't work. Thanks in advance.
arik
  • 28,170
  • 36
  • 100
  • 156
5
votes
3 answers

How do you efficiently find a union of a list of lists of values in haskell?

Since a code example is worth a thousand words I'll start with that: testList = [1,2,2,3,4,5] testSet = map sumMapper $ tails testList where sumMapper [] = [] sumMapper (a:b) = sumMap a b sumMap a b = map (+…
Mike H-R
  • 7,726
  • 5
  • 43
  • 65
5
votes
1 answer

Get/Set from C# to Java

I'm working on a project to translate a c# project to Java. I have the following Get/Set block in C# public Unit[] Units { get { Unit[] units_aux = new Unit[this.list_units.Count]; this.list_units.CopyTo(units_aux); …
Ichigo-San
  • 79
  • 1
  • 8
5
votes
2 answers

SET Command - Floating point numbers?

How do I use the SET command in windows for floating point arithmatic. /A stands for arithmetic and %VAR% prints the data of VAR not the name. For example when I do: SET /A VAR="2.5+3.1" ECHO %VAR% pause I get the error: 'Missing Operator'. The…
user2912448
5
votes
4 answers

How to use java empty HashSet in if-statement?

public static void main(String[] args) { Map> test = new HashMap>(); test.put("1", new HashSet()); System.out.println(test); System.out.println(test.get("1")); …
user2995344
  • 268
  • 1
  • 3
  • 14
5
votes
2 answers

List.Add vs HashSet.Add for small collections in c#

Given HashSet set; List list; T t; Which of these performs better for SMALL collections? if (! list.Contains (t)) list.Add (t); Or set.Add (t); Source of doubt: HashSet vs. List performance
Cristian Garcia
  • 9,630
  • 6
  • 54
  • 75