C# Program to return the difference between two sequences

In C#, you can find the difference between two sequences using the Except() method from LINQ. This method returns elements from the first sequence that do not appear in the second sequence, effectively performing a set difference operation.

Syntax

Following is the syntax for using the Except() method −

IEnumerable<T> result = firstSequence.Except(secondSequence);

Parameters

  • firstSequence − The sequence to return elements from.

  • secondSequence − The sequence whose elements to exclude from the returned sequence.

Return Value

The method returns an IEnumerable<T> containing the elements from the first sequence that are not present in the second sequence.

Using Except() with Arrays

The most common scenario is finding elements that exist in one array but not in another −

using System;
using System.Linq;
using System.Collections.Generic;

class Demo {
   static void Main() {
      double[] arr1 = { 10.2, 15.6, 23.3, 30.5, 50.2 };
      double[] arr2 = { 15.6, 30.5, 50.2 };
      
      Console.WriteLine("First Array:");
      foreach(double ele in arr1) {
         Console.WriteLine(ele);
      }
      
      Console.WriteLine("\nSecond Array:");
      foreach(double ele in arr2) {
         Console.WriteLine(ele);
      }
      
      IEnumerable<double> difference = arr1.Except(arr2);
      
      Console.WriteLine("\nDifference (arr1 - arr2):");
      foreach (double a in difference) {
         Console.WriteLine(a);
      }
   }
}

The output of the above code is −

First Array:
10.2
15.6
23.3
30.5
50.2

Second Array:
15.6
30.5
50.2

Difference (arr1 - arr2):
10.2
23.3

Using Except() with Different Data Types

The Except() method works with any data type. Here's an example with strings −

using System;
using System.Linq;
using System.Collections.Generic;

class Program {
   static void Main() {
      string[] languages1 = { "C#", "Java", "Python", "JavaScript", "C++" };
      string[] languages2 = { "Java", "JavaScript" };
      
      IEnumerable<string> result = languages1.Except(languages2);
      
      Console.WriteLine("Languages not in the second array:");
      foreach (string lang in result) {
         Console.WriteLine(lang);
      }
   }
}

The output of the above code is −

Languages not in the second array:
C#
Python
C++

Bidirectional Difference

To find elements that are in either sequence but not in both (symmetric difference), you can combine Except() operations −

using System;
using System.Linq;
using System.Collections.Generic;

class Program {
   static void Main() {
      int[] set1 = { 1, 2, 3, 4, 5 };
      int[] set2 = { 4, 5, 6, 7, 8 };
      
      IEnumerable<int> onlyInSet1 = set1.Except(set2);
      IEnumerable<int> onlyInSet2 = set2.Except(set1);
      IEnumerable<int> symmetricDiff = onlyInSet1.Union(onlyInSet2);
      
      Console.WriteLine("Elements only in first set: " + string.Join(", ", onlyInSet1));
      Console.WriteLine("Elements only in second set: " + string.Join(", ", onlyInSet2));
      Console.WriteLine("Symmetric difference: " + string.Join(", ", symmetricDiff));
   }
}

The output of the above code is −

Elements only in first set: 1, 2, 3
Elements only in second set: 6, 7, 8
Symmetric difference: 1, 2, 3, 6, 7, 8

Key Points

  • The Except() method uses the default equality comparer to compare elements.

  • The order of elements in the result follows the order in the first sequence.

  • Duplicate elements in the first sequence are automatically removed from the result.

  • The operation is not commutative: A.Except(B) is different from B.Except(A).

Conclusion

The Except() method in C# provides an efficient way to find the difference between two sequences by returning elements from the first sequence that are not present in the second. It's particularly useful for set operations and filtering collections based on exclusion criteria.

Updated on: 2026-03-17T07:04:35+05:30

295 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements