Questions tagged [symmetric-difference]

The symmetric difference of two sets consists of those elements that are present in either set, but not in both.

"Symmetric difference" is a mathematical operation defined upon sets.
The symmetric difference between two sets is defined as the set of those elements that belong to either one of those sets, but not both.

For example, let A = { 1, 2, 3, 4 } and B = { 2, 4, 6, 8 }.

  • Elements in A that are not in B are 1 and 3.
  • Elements that are in B but not in A are 6 and 8.
  • Hence, the symmetric difference is { 1, 3, 6, 8 }.

Symmetric difference can also be defined on more than two sets. In this case, the symmetric difference is defined as the set of elements which are in an odd number of sets.

Wikipedia link: https://en.wikipedia.org/wiki/Symmetric_difference

25 questions
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
20
votes
21 answers

Trying to solve symmetric difference using Javascript

I am trying to figure out a solution for symmetric difference using javascript that accomplishes the following objectives: accepts an unspecified number of arrays as arguments preserves the original order of the numbers in the arrays does not…
davisec52
  • 357
  • 1
  • 2
  • 8
10
votes
4 answers

Function to find symmetric difference (opposite of intersection) in R?

The Problem I have two string vectors of different lengths. Each vector has a different set of strings. I want to find the strings that are in one vector but not in both; that is, the symmetric difference. Analysis I looked at the function setdiff,…
Gyan Veda
  • 6,309
  • 11
  • 41
  • 66
5
votes
4 answers

Comparing 2 arrays for differences (find symmetric difference)

This should be a simple algorithm but I can't wrap my head around the logic. I need to take 2 arrays and keep only the values that are found in one array or the other and not both. For example if I have arr1 [1,2,3,6] arr2 [2,3,4,5,7] I want to…
Crash667
  • 343
  • 2
  • 16
4
votes
3 answers

Python: Symmetrical Difference Between List of Sets of Strings

I have a list that contains multiple sets of strings, and I would like to find the symmetric difference between each string and the other strings in the set. For example, I have the following list: targets = [{'B', 'C', 'A'}, {'E', 'C', 'D'},…
SummerEla
  • 1,902
  • 3
  • 26
  • 43
4
votes
2 answers

What is Python symmetric_difference and how is it different from XOR operation?

While learning Python from http://www.learnpython.org/en/Sets I encountered the notion of symmetric_difference between sets. I thought it gave the same output as 'exclusive or' operations on sets. How is it different?
Huxwell
  • 171
  • 2
  • 14
2
votes
1 answer

How can the symmetric difference between two lists be obtained?

I would like to know how to get the symmetric difference between two lists. For example: list1 = ['a','a','b','c','d'] list2 = ['a','b','c','f'] sym_dif = ['a', 'd', 'f'] Sets don't work because I have multiple instances of the same object…
Her0Her0
  • 486
  • 1
  • 4
  • 12
2
votes
4 answers

Find symmetric difference between two arrays

I'd like to find the symmetric difference between TWO arrays. This implementation works, however I'd like to write a function that is specific to only two arrays, rather than one that finds the symmetric difference between a bunch of arrays. The…
vince
  • 7,808
  • 3
  • 34
  • 41
1
vote
1 answer

how to optimize search difference between array / list of object

Premesis: I am using ActionScript with two arraycollections containing objects with values to be matched... I need a solution for this (if in the framework there is a library that does it better) otherwise any suggestions are appreciated... Let's…
1
vote
3 answers

Prolog lists difference

I'm trying to make program in prolog that will do something like this: diffSet([a,b,c,d], [a,b,e,f], X). X = [c,d,e,f] I wrote this: diffSet([], _, []). diffSet([H|T1],Set,Z):- member(Set, H), !, diffSet(T1,Set,Z). diffSet([H|T], Set, [H|Set2]):-…
Adrian Modliszewski
  • 1,114
  • 2
  • 18
  • 31
1
vote
2 answers

Can I use update method of set in Python to merge two set (x-y) and (y-x)?

I have these two source code and I do not understand the difference x = {"apple", "banana", "cherry"} y = {"google", "microsoft", "apple"} z = x.symmetric_difference(y) print(z) ## z now is {'google', 'cherry', 'microsoft', 'banana'} and x =…
TXT
  • 11
  • 1
1
vote
1 answer

What is the best effective way to get symmetric difference between two strings(in python)?

Example: If there are two strings:- s1 = 'cde' s2 = 'abc' Output: 'deab' I converted the string into a list and compared two lists. a = 'cde' b = 'abc' list1 = list(a) list2 = list(b) diff = [] for item in list1: if item not in list2: …
1
vote
1 answer

Concurrent symmetric difference

I have tutorials on my university about cuncurrent programming. My task is to write a program based on semaphores in which a symmetric difference of two sets of numbers will be computed. I can't see where the concurrent programming is necessary. I…
1
vote
1 answer

Why are the symmetric_difference and intersection operations for the Shapely.Geometry library apparently inconsistent?

I am using Shapely in a python script to determine the intersection between LinearRings and symmetric difference between Polygons in 2D. I am running into what I assume are tolerance issues, and I cannot find any information in the Shapely reference…
Alex Smith
  • 1,096
  • 8
  • 12
1
vote
1 answer

Can a RegEx with negative lookahead be represented as a finite automaton?

I'm working on a tool for manipulating context-free languages, and the internal representation of a grammar is stored as a finite automaton. Looking farther into EBNF and RegEx, I learned that EBNF has "exceptions" and RegEx has negative lookahead.…
Brent
  • 4,153
  • 4
  • 30
  • 63
1
2