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").
12,105
questions
1242
votes
33
answers
1.5m
views
Get difference between two lists with Unique Entries
I have two lists in Python:
temp1 = ['One', 'Two', 'Three', 'Four']
temp2 = ['One', 'Two']
Assuming the elements in each list are unique, I want to create a third list with items from the first list ...
837
votes
19
answers
788k
views
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[...
748
votes
16
answers
621k
views
Does Python have an ordered set?
Python has an ordered dictionary. What about an ordered set?
677
votes
15
answers
893k
views
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 - ...
613
votes
7
answers
270k
views
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()?
611
votes
15
answers
1.1m
views
Convert Set to List without creating new List
I am using this code to convert a Set to a List:
Map<String, List<String>> mainMap = new HashMap<>();
for (int i=0; i < something.size(); i++) {
Set<String> set = getSet(...
569
votes
9
answers
754k
views
Append values to a set in Python
How do I add values to an existing set?
569
votes
7
answers
486k
views
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 ...
514
votes
26
answers
685k
views
What is the difference between Set and List?
What is the fundamental difference between the Set<E> and List<E> interfaces?
470
votes
12
answers
538k
views
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()
438
votes
22
answers
1.0m
views
Getting an element from a Set
Why doesn't Set provide an operation to get an element that equals another element?
Set<Foo> set = ...;
...
Foo foo = new Foo(1, 2, 3);
Foo bar = set.get(foo); // get the Foo element from the ...
434
votes
9
answers
1.1m
views
How to get the first element of the List or Set? [duplicate]
I'd like to know if I can get the first element of a list or set. Which method to use?
416
votes
13
answers
589k
views
Add list to set
How do I add a list of values to an existing set?
398
votes
7
answers
266k
views
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, ...
339
votes
13
answers
229k
views
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?
337
votes
4
answers
238k
views
golang why don't we have a set datastructure [closed]
I'm trying to solve "The go programming lanaguage" exercise #1.4 which requires me to have a set. I can create a set type but why doesn't the language come with one ? go, having come from google, ...
328
votes
9
answers
880k
views
How to Iterate over a Set/HashSet without an Iterator?
How can I iterate over a Set/HashSet without the following?
Iterator iter = set.iterator();
while (iter.hasNext()) {
System.out.println(iter.next());
}
321
votes
6
answers
429k
views
JavaScript Array to Set
MDN references JavaScript's Set collection abstraction. I've got an array of objects that I'd like to convert to a set so that I am able to remove (.delete()) various elements by name:
var array = [
...
318
votes
15
answers
252k
views
Simplest way to merge ES6 Maps/Sets?
Is there a simple way to merge ES6 Maps together (like Object.assign)? And while we're at it, what about ES6 Sets (like Array.concat)?
295
votes
2
answers
298k
views
How to calculate the intersection of two sets? [duplicate]
Possible Duplicate:
Efficiently finding the intersection of a variable number of sets of strings
Say, have two Hashset, how to calculate the intersection of them?
Set<String> s1 = new ...
282
votes
11
answers
104k
views
How to customize object equality for JavaScript Set
New ES 6 (Harmony) introduces new Set object. Identity algorithm used by Set is similar to === operator and so not much suitable for comparing objects:
var set = new Set();
set.add({a:1});
set.add({a:...
275
votes
8
answers
598k
views
How to construct a set out of list items in python?
I have a list of filenames in python and I would want to construct a set out of all the filenames.
filelist=[]
for filename in filelist:
set(filename)
This does not seem to work. How can do this?...
271
votes
6
answers
182k
views
How to "perfectly" override a dict?
How can I make as "perfect" a subclass of dict as possible? The end goal is to have a simple dict in which the keys are lowercase.
It would seem that there should be some tiny set of primitives I can ...
268
votes
5
answers
152k
views
How to map/reduce/filter a Set in JavaScript?
Is there any way to map/reduce/filter/etc a Set in JavaScript or will I have to write my own?
Here's some sensible Set.prototype extensions
Set.prototype.map = function map(f) {
var newSet = new ...
262
votes
11
answers
251k
views
Python Sets vs Lists
In Python, which data structure is more efficient/speedy? Assuming that order is not important to me and I would be checking for duplicates anyway, is a Python set slower than a Python list?
256
votes
6
answers
164k
views
Swift Set to Array
An NSSet can be converted to Array using set.allObjects() but there is no such method in the new Set (introduced with Swift 1.2). It can still be done by converting Swift Set to NSSet and use the ...
253
votes
11
answers
282k
views
Getting the difference between two sets
So if I have two sets:
Set<Integer> test1 = new HashSet<Integer>();
test1.add(1);
test1.add(2);
test1.add(3);
Set<Integer> test2 = new HashSet<Integer>();
test2.add(1);
test2....
248
votes
19
answers
209k
views
Why doesn't java.util.Set have get(int index)?
Why does the java.util.Set interface lack get(int Index), or any similar get() method?
It seems that sets are great for putting things into, but I can't find an elegant way of retrieving a single item ...
244
votes
14
answers
295k
views
Java Set retain order?
Does a Java Set retain order? A method is returning a Set to me and supposedly the data is ordered but iterating over the Set, the data is unordered. Is there a better way to manage this? Does the ...
234
votes
6
answers
116k
views
How is set() implemented?
I've seen people say that set objects in python have O(1) membership-checking. How are they implemented internally to allow this? What sort of data structure does it use? What other implications does ...
232
votes
11
answers
206k
views
How to add an array of values to a Set
The old school way of adding all values of an array into the Set is:
// for the sake of this example imagine this set was created somewhere else
// and I cannot construct a new one out of an array
...
229
votes
17
answers
283k
views
Converting a list to a set changes element order
Recently I noticed that when I am converting a list to set the order of elements is changed and is sorted by character.
Consider this example:
x=[1,2,20,6,210]
print(x)
# [1, 2, 20, 6, 210] # the ...
227
votes
12
answers
331k
views
How to JSON serialize sets? [duplicate]
I have a Python set that contains objects with __hash__ and __eq__ methods in order to make certain no duplicates are included in the collection.
I need to json encode this result set, but passing ...
221
votes
9
answers
365k
views
How to join two sets in one line without using "|"
Assume that S and T are assigned sets. Without using the join operator |, how can I find the union of the two sets? This, for example, finds the intersection:
S = {1, 2, 3, 4}
T = {3, 4, 5, 6}
...
219
votes
19
answers
117k
views
comparing ECMA6 sets for equality
How do you compare two javascript sets? I tried using == and === but both return false.
a = new Set([1,2,3]);
b = new Set([1,3,2]);
a == b; //=> false
a === b; //=> false
These two sets are ...
216
votes
34
answers
263k
views
Picking a random element from a set
How do I pick a random element from a set?
I'm particularly interested in picking a random element from a
HashSet or a LinkedHashSet, in Java.
213
votes
6
answers
229k
views
Most concise way to convert a Set<T> to a List<T>
For example, I am currently doing this:
Set<String> setOfTopicAuthors = ....
List<String> list = Arrays.asList(
setOfTopicAuthors.toArray( new String[0] ) );
Can you beat this ?
206
votes
32
answers
252k
views
How to get all subsets of a set? (powerset) [duplicate]
Given a set
{0, 1, 2, 3}
How can I produce the subsets:
[set(),
{0},
{1},
{2},
{3},
{0, 1},
{0, 2},
{0, 3},
{1, 2},
{1, 3},
{2, 3},
{0, 1, 2},
{0, 1, 3},
{0, 2, 3},
{1, 2, 3},
{0, 1,...
206
votes
18
answers
723k
views
Set value of hidden field in a form using jQuery's ".val()" doesn't work
I've been trying to set the value of a hidden field in a form using jQuery, but without success.
Here is a sample code that explains the problem. If I keep the input type to "text", it works without ...
200
votes
22
answers
519k
views
How to set background color of a View
I'm trying to set the background color of a View (in this case a Button).
I use this code:
// set the background to green
v.setBackgroundColor(0x0000FF00 );
v.invalidate();
It causes the Button to ...
195
votes
2
answers
373k
views
Sorting a set of values
I have values like this:
x = set(['0.000000000', '0.009518000', '10.277200999', '0.030810999', '0.018384000', '4.918560000'])
y = set(['4.918859000', '0.060758000', '4.917336999', '0.003949999', '0....
194
votes
5
answers
132k
views
How do I add the contents of an iterable to a set?
What is the "one [...] obvious way" to add all items of an iterable to an existing set?
194
votes
8
answers
131k
views
Deleting elements from std::set while iterating
I need to go through a set and remove elements that meet a predefined criteria.
This is the test code I wrote:
#include <set>
#include <algorithm>
void printElement(int value) {
std:...
187
votes
3
answers
404k
views
Best way to convert list to comma separated string in java [duplicate]
I have Set<String> result & would like to convert it to comma separated string. My approach would be as shown below, but looking for other opinion as well.
List<String> slist = new ...
182
votes
7
answers
403k
views
Python set to list
How can I convert a set to a list in Python? Using
a = set(["Blah", "Hello"])
a = list(a)
doesn't work. It gives me:
TypeError: 'set' object is not callable
176
votes
20
answers
288k
views
Is there hash code function accepting any object type?
Basically, I'm trying to create an object of unique objects, a set. I had the brilliant idea of just using a JavaScript object with objects for the property names. Such as,
set[obj] = true;
This ...
168
votes
5
answers
22k
views
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? ...
168
votes
9
answers
427k
views
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 ...
164
votes
5
answers
196k
views
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'}
161
votes
5
answers
283k
views
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 ...