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

Why isn't there a "set" interface in the .NET framework?

I'm thinking specifically about the generic class HashSet. It implements several interfaces, but none exposes the correct semantics of a set. Specifically, none supports an Add method returning bool. (ICollection supports void Add, which can…
Sean Devlin
  • 1,662
  • 12
  • 17
5
votes
1 answer

Change Multiple Image Sources Javascript

I am creating a simple image gallery, and I would like to create multiple sets of images. On the click of a link all of the images in the link's given set will change. Here is my current code:
5
votes
5 answers

Divide a list of numbers into smaller list with "sum" approximately same

I execute around 2000 tests on grid, each test being run as separate task on grid. The tests do have rather big startup time. Total execution takes 500 hours, finishes in less than 10 hours on 60 node SunGridEngine. Runtime of tests vary from 5…
Jayan
  • 18,003
  • 15
  • 89
  • 143
5
votes
2 answers

How to pass list of sets as separate arguments into function?

I've created a list of sets that I'd like to pass into set.intersection() For example: List_of_Sets = [{1,2,3},{3,4,5},{5,6,7}] set.intersection(List_of_Sets) Result: TypeError: descriptor 'intersection' requires a 'set' object but received a…
Chris
  • 5,444
  • 16
  • 63
  • 119
5
votes
4 answers

Efficient way to find the min and max value of a changing set in python

I need to find the min/max value in a changing large set, in C++, it could be #include using namespace std; int minVal(set & mySet){ return *mySet.begin(); } int maxVal(set & mySet){ return *mySet.rbegin(); } int main(){ …
asmn
  • 127
  • 2
  • 5
5
votes
6 answers

All subsets of a set in clojure

I wish to generate all subsets of a set except empty set ie (all-subsets #{1 2 3}) => #{#{1},#{2},#{3},#{1,2},#{2,3},#{3,1},#{1,2,3}} How can this be done in clojure?
zcaudate
  • 13,998
  • 7
  • 64
  • 124
5
votes
2 answers

A set implementation in LaTeX?

Consider the following straightforward implementation of a list in latex: \newcommand{\add@to@list}[2]{% \ifx#2\@empty% \xdef#2{#1}% \else% \xdef#2{#2,#1}% \fi% }% I wonder if there is a simple way to implement a set (list with no…
Giovanni Funchal
  • 8,934
  • 13
  • 61
  • 110
5
votes
3 answers

How to store set difference s1-s2 in s1 without temporary set?

Does STL already contain any simple method or algorithm for storing the difference between to sets set1 and set2 directly in set1, without the need for a temporary set variable? The sample code below shows some alternatives that I already have tried…
user128300
5
votes
3 answers

Map Sets in Golang

If I have a struct like: type Foo struct { title string Tags map[string]string } How might approach maintaining a unique set of such structs? From what I understand, although struct equality is a thing - map equality isn't. This means I can't…
Kyle Brandt
  • 26,938
  • 37
  • 124
  • 165
5
votes
4 answers

How to set the Content-Type header using JavaScript

How can you set the Content-Type header to "application/x-www-form-urlencoded; charset=UTF-8" using JavaScript? I need to do this so I can view a form with french characters without generating errors. Thanks
Bob K
  • 61
  • 1
  • 1
  • 3
5
votes
4 answers

How to avoid stack overflow errors when defining set accessor in C#

People of stackoverflow. I am new to c# and this is the first time I have not been able to find an answer to one of my elementary questions. Who can help me?!I am trying to define set logic for a public instance field. This runs flawlessly, public…
5
votes
3 answers

Why are my form variables not passing through POST?

I am sending form data through POST, but the corresponding POST variables are not set, and do not. Also, when I store POST data into local PHP variables, I seem to be unable to use those variables. (Once I resolve the first issue, I have a feeling I…
ahnbizcad
  • 10,491
  • 9
  • 59
  • 85
5
votes
1 answer

Postgresql fastest way of getting the set of unique values in one column or a large table

I have a constantly growing, potentially very large table in a Postgresql database that contains "data" from different "channels" for different "devices" eg.: Table data: id (PK) device_id (FK -> device) channel_id (FK -> channel) timestamp…
Tim
  • 1,272
  • 11
  • 28
5
votes
2 answers

Need help in my "Custom Set" implementation

Okay I have a really annoying error. Its coming from my retainAll method. The problem is that I am outputting 1,3,5 in ints at the end, but I need 1,3,5,7,9. Here is the code below for the MySet and driver classes public class MySetTester { …
georgetheevilman
  • 177
  • 4
  • 10
5
votes
2 answers

Python: how to remove duplicates from a list using a set (order is important)

so I have this list: a = [-11, 13, 13, 10, -11, 10, 9, -3, 6, -9, -6, -6, 13, 8, -11, -5, 6, -8, -12, 5, -9, -1, -5, 2, -2, 13, 14, -9, 7, -4] and by using a set I need to remove the duplicates and also keep them in the same order I used this…
pixshi
  • 159
  • 1
  • 3
  • 6