Questions tagged [set-difference]

The difference between two sets A and B consists of all elements that are in A but not in B.

Set difference is a mathematical operation defined on sets. It is also known as the relative complement of B with respect to A.
If we have two sets, say A and B, their difference A - B is the set of all elements that are in A but not in B.

Several programming languages have built-in features to remove a collection of elements from another collection of elements. When both collections are sets (meaning each element can occur at most once per collection and the order does not matter), this implements set difference.

Source: Wikipedia

197 questions
1221
votes
33 answers

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 which are not in the second list: temp3 =…
Max Frai
  • 61,946
  • 78
  • 197
  • 306
200
votes
14 answers

Select rows from one data.frame that are not present in a second data.frame

I have two data.frames: a1 <- data.frame(a = 1:5, b=letters[1:5]) a2 <- data.frame(a = 1:3, b=letters[1:3]) I want to find the rows a1 have that a2 doesn't. Is there a built in function for this type of operation? (p.s: I did write a solution for…
Tal Galili
  • 24,605
  • 44
  • 129
  • 187
180
votes
15 answers

What is the fastest or most elegant way to compute a set difference using Javascript arrays?

Let A and B be two sets. I'm looking for really fast or elegant ways to compute the set difference (A - B or A \B, depending on your preference) between them. The two sets are stored and manipulated as Javascript arrays, as the title…
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
116
votes
3 answers

Set difference versus set subtraction

What distinguishes - and .difference() on sets? Obviously the syntax is not the same. One is a binary operator, and the other is an instance method. What else? s1 = set([1,2,3]) s2 = set([3,4,5]) >>> s1 - s2 set([1, 2]) >>>…
David542
  • 104,438
  • 178
  • 489
  • 842
86
votes
7 answers

bash, Linux: Set difference between two text files

I have two files A-nodes_to_delete and B-nodes_to_keep. Each file has a many lines with numeric ids. I want to have the list of numeric ids that are in nodes_to_delete but NOT in nodes_to_keep, i.e. A\B Doing it within a PostgreSQL database is…
Adam Matan
  • 128,757
  • 147
  • 397
  • 562
85
votes
10 answers

c++ STL set difference

Does the C++ STL set data structure have a set difference operator?
Steve
  • 1,955
  • 9
  • 28
  • 30
51
votes
7 answers

Difference and intersection of two arrays containing objects

I have two arrays list1 and list2 which have objects with some properties; userId is the Id or unique property: list1 = [ { userId: 1234, userName: 'XYZ' }, { userId: 1235, userName: 'ABC' }, { userId: 1236, userName: 'IJKL' }, {…
Shashi
  • 1,112
  • 2
  • 17
  • 32
47
votes
3 answers

MySQL: difference of two result sets

How can I get the set difference of two result sets? Say I have a result set (just one column in each): result1: 'a' 'b' 'c' result2: 'b' 'c' I want to minus what is in result1 by result2: result1 - result2 such that it equals: difference of…
Zombies
  • 25,039
  • 43
  • 140
  • 225
38
votes
3 answers

Python sets: difference() vs symmetric_difference()

What is the difference between difference() and symmetric_difference() methods in python sets?
Anya Samadi
  • 673
  • 1
  • 5
  • 13
29
votes
5 answers

Exclude characters from a character class

Is there a simple way to match all characters in a class except a certain set of them? For example if in a lanaguage where I can use \w to match the set of all unicode word characters, is there a way to just exclude a character like an underscore…
Dan Roberts
  • 4,664
  • 3
  • 34
  • 43
28
votes
4 answers

Get distinct list between two lists in C#

I have two lists of strings. How do I get the list of distinct values between them or remove the second list elements from the first list? List list1 = { "see","you","live"} List list2 = { "see"} The result should be…
kartal
  • 17,436
  • 34
  • 100
  • 145
24
votes
1 answer

How to do sane "set-difference" in Ruby?

Demo (I expect result [3]): [1,2] - [1,2,3] => [] # Hmm [1,2,3] - [1,2] => [3] # I see a = [1,2].to_set => # b = [1,2,3].to_set => # a - b => # WTF! And: [1,2,9] - [1,2,3] => [9] # Hmm.…
Zabba
  • 64,285
  • 47
  • 179
  • 207
20
votes
7 answers

How to receive difference of maps in java?

I have two maps: Map map1; Map map2; I need to receive difference between these maps. Does exist may be apache utils how to receive this difference? For now seems need take entry set of each map and found diff1 =…
user710818
  • 23,228
  • 58
  • 149
  • 207
20
votes
3 answers

Find the set difference between two large arrays (matrices) in Python

I have two large 2-d arrays and I'd like to find their set difference taking their rows as elements. In Matlab, the code for this would be setdiff(A,B,'rows'). The arrays are large enough that the obvious looping methods I could think of take too…
zss
  • 335
  • 2
  • 3
  • 10
16
votes
3 answers

Trying to understand "except all" in sql query

I came across this example and I don't understand what it means. (SELECT drinker FROM Frequents) EXCEPT ALL (SELECT drinker FROM Likes); relations: Frequents(drinker, bar), Likes(drinker, beer) What does the ALL do in this case? How is the…
1
2 3
13 14