## Get the symmetric difference from generic lists

I have 2 separate List and I need to compare the two and get everything but the intersection of the two lists. How can I do this (C#)?

You can use Except to get everything but the intersection of the two lists.

```var differences = listA.Except(listB).Union(listB.Except(listA));
```

If you want to get everything but the union:

```var allButUnion = new List<MyClass>();
```

(The union is everything in both lists - everything but the union is the empty set...)

Symmetric difference, java get difference between two collections java set I'm wondering if there is a quick/clean way to get the symmetric difference between two sets ? I have: Guava is a bit more modern, supporting generics, but either of these will work. That is, enumerate the items that are in A or B but not both. This set is called the symmetric difference of A and B.

If you mean the set of everything but the intersection (symmetric difference) you can try:

```var set = new HashSet<Type>(list1);
set.SymmetricExceptWith(list2);
```

What is the best way get the symmetric difference between two sets , Set operations: union, intersection, difference, symmetric difference, is subset, is superset : Set � Collections Data Structure � Java. Get the intersection of set1 and set2, set1 becomes the intersection of set1 and set2. 25. A NumberedSet is a generic container of Objects where each element is identified by an integer id . The Python symmetric_difference () method returns the symmetric difference of two sets. The symmetric difference of two sets A and B is the set of elements that are in either A or B, but not in their intersection. Symmetric difference of two sets The syntax of symmetric_difference () is:

Do you mean everything that's only in one list or the other? How about:

```var allButIntersection = a.Union(b).Except(a.Intersect(b));
```

That's likely to be somewhat inefficient, but it fairly simply indicates what you mean (assuming I've interpreted you correctly, of course).

Set operations: union, intersection, difference, symmetric difference , In Python, we can also find the symmetric difference using the ^ operator. A = {'a', 'b', 'c', 'd'}� Use set () to find the difference of two lists In this approach, we’ll first derive two SETs (say set1 and set2) from the LISTs (say list1 and list2) by passing them to set () function. After that, we’ll perform the set difference operation. It will return those elements from list1 which don’t exist in the second.

Something like this?

```String[] one = new String[] { "Merry", "Metal", "Median", "Medium", "Malfunction", "Mean", "Measure", "Melt", "Merit", "Metaphysical", "Mental", "Menial", "Mend", "Find" };
String[] two = new String[] { "Merry", "Metal", "Find", "Puncture", "Revise", "Clamp", "Menial" };

List<String> tmp = one.Except(two).ToList();

String[] result = tmp.ToArray();
```

Python Set symmetric_difference(), Generic Instance Method. symmetricDifference(_:). Returns a new set with the elements that are either in this set or in the given sequence, but not in both. If there are a set_A and set_B, then the symmetric difference between them will be equal to the union of set_A and set_B without the intersection between the two. // Takes a single parameter that has to be // a set and returns a new set which is the // symmetric difference between the two sets.

Here is a generic Extension method. Rosetta Code uses Concat, and Djeefther Souza says it's more efficient.

```public static class LINQSetExtensions
{
// Made aware of the name for this from Swift
// https://stackoverflow.com/questions/1683147/get-the-symmetric-difference-from-generic-lists
// Generic implementation adapted from https://www.rosettacode.org/wiki/Symmetric_difference
public static IEnumerable<T> SymmetricDifference<T>(this IEnumerable<T> first, IEnumerable<T> second)
{
// I've used Union in the past, but I suppose Concat works.
// No idea if they perform differently.
return first.Except(second).Concat(second.Except(first));
}
}
```

I haven't actually benchmarked it. I think it would depend on how Union vs. Concat are implemented. In my dreamworld, .NET uses a different algorithm depending on data type or set size, though for IEnumerable it can't determine set size in advance.

Also, you can pretty much ignore my answer - Jon Skeet says that the HashSet method "Excellent - that looks like the best way of doing it to me."

symmetricDifference(_:), In addition to the integer set abstract type, a well-formed set is a list of pairs of exact integers, Further, adjacent ranges must have at least one integer between them. An integer set implements the stream and sequence generic interfaces. Returns the difference of the given sets (i.e., elements in x that are not in y). In this problem given two lists. Our tasks is to display difference between two lists. Python provides set() method. We use this method here. A set is an unordered collection with no duplicate elements. Set objects also support mathematical operations like union, intersection, difference, and symmetric difference. Example

8 Integer Sets, retrieve from a collection. Note. Swift's array, set, and dictionary types are implemented as generic collections. For more about generic types and collections,� Return a set that contains all items from both sets, except items that are present in both sets: x = {"apple", "banana", "cherry"} y = {"google", "microsoft", "apple"} z = x.symmetric_difference (y)

Collection Types — The Swift Programming Language (Swift 5.3), and binary operators accept generic Enumerable objects besides sets and arrays. An Enumerable object can be converted to Set using the to_set method. operator is intentionally left out because not every pair of sets is comparable ({x, y } vs. Returns true if the set and the given set have no element in common. Set symmetric_difference () Method symmetric_difference () method is used to get the list of all elements which are not common in both sets, the method is called with this set (set1) and another set (set2) is supplied as an argument.

Class: Set (Ruby 2.7.1), When should I use Lists vs Sets in Java Collection List is an ordered sequence of But, no new methods are defined inside Set interface, so we have to use� Using set.symmetric_difference() to get differences between two lists. In all the previous solutions, we got all the differences between two lists in two steps. But using symmetric_difference() we can achieve that in a single step. set.symmetric_difference(seq)