FREE E LEARNING PLATFORM
HOMEEXCEPTIONSOOPSJVMINTRO
 

Symmetric Difference of Two Sets using C, C++, Java and Python




❮ Previous    Next ❯


The Symmetric Difference of Two Sets is an important operation in Set Theory. The symmetric difference contains all those elements which belong to either of the two sets, but do not belong to both sets. In other words, common elements are removed and only the elements that are unique to either set are retained. In this experiment, we shall create two sets and perform the Symmetric Difference operation using C, C++, Java and Python.


Objective

To write a program in C, C++, Java and Python to create two sets and perform the Symmetric Difference operation on the sets.


Theory

The Symmetric Difference of two sets A and B is represented as:

A △ B

It contains all elements which belong to either Set A or Set B but not to both sets. Mathematically,

A △ B = (A − B) ∪ (B − A)

It can also be represented as:

A △ B = (A ∪ B) − (A ∩ B)


Example

Consider the following two sets:

A = {1, 2, 3, 4}

B = {3, 4, 5, 6}

The common elements are 3 and 4. Therefore, these elements are excluded from the Symmetric Difference.

A − B = {1, 2}

B − A = {5, 6}

Therefore:

A △ B = {1, 2, 5, 6}

Important Concept

The Symmetric Difference contains elements that are present in exactly one of the two sets. If an element is present in both sets, it is not included in the result.


Alternative Definition

The Symmetric Difference can also be calculated using Union and Intersection:

A △ B = (A ∪ B) − (A ∩ B)

That means:

  1. Find the Union of A and B.
  2. Find the Intersection of A and B.
  3. Remove the common elements from the Union.
  4. The remaining elements form the Symmetric Difference.

Algorithm

  1. Start.
  2. Read the elements of Set A.
  3. Read the elements of Set B.
  4. Take each element of Set A.
  5. Check whether the element is present in Set B.
  6. If it is not present in Set B, add it to the result.
  7. Take each element of Set B.
  8. Check whether the element is present in Set A.
  9. If it is not present in Set A, add it to the result.
  10. Display the Symmetric Difference Set.
  11. Stop.

Flowchart

Symmetric Difference of Two Sets Flowchart


Example Table

Operation Result
Set A {1, 2, 3, 4}
Set B {3, 4, 5, 6}
A − B {1, 2}
B − A {5, 6}
A △ B {1, 2, 5, 6}


C Program

The following C program finds the Symmetric Difference of two sets without using any built-in set functions.


/* C Program to find Symmetric Difference of Two Sets */

#include <stdio.h>

int main()
{
    int A[100], B[100], result[200];
    int n, m;
    int i, j, k = 0;
    int found;

    printf("Enter number of elements in Set A: ");
    scanf("%d", &n);

    printf("Enter elements of Set A:\n");

    for(i = 0; i < n; i++)
    {
        scanf("%d", &A[i]);
    }

    printf("Enter number of elements in Set B: ");
    scanf("%d", &m);

    printf("Enter elements of Set B:\n");

    for(i = 0; i < m; i++)
    {
        scanf("%d", &B[i]);
    }

    /*
       Find A - B
       Elements present in A but not in B
    */

    for(i = 0; i < n; i++)
    {
        found = 0;

        for(j = 0; j < m; j++)
        {
            if(A[i] == B[j])
            {
                found = 1;
                break;
            }
        }

        if(found == 0)
        {
            result[k] = A[i];
            k++;
        }
    }

    /*
       Find B - A
       Elements present in B but not in A
    */

    for(i = 0; i < m; i++)
    {
        found = 0;

        for(j = 0; j < n; j++)
        {
            if(B[i] == A[j])
            {
                found = 1;
                break;
            }
        }

        if(found == 0)
        {
            result[k] = B[i];
            k++;
        }
    }

    printf("\nSymmetric Difference of A and B:\n");

    if(k == 0)
    {
        printf("Empty Set");
    }
    else
    {
        for(i = 0; i < k; i++)
        {
            printf("%d ", result[i]);
        }
    }

    return 0;
}

Sample Output (C)

Enter number of elements in Set A: 4

Enter elements of Set A:
1 2 3 4

Enter number of elements in Set B: 4

Enter elements of Set B:
3 4 5 6

Symmetric Difference of A and B:
1 2 5 6

How the C Program Works

  1. The program reads Set A and Set B.
  2. It first finds elements present in A but not in B.
  3. These elements represent A − B.
  4. It then finds elements present in B but not in A.
  5. These elements represent B − A.
  6. Both results are stored in the result array.
  7. The result array represents the Symmetric Difference.

C++ Program

The following C++ program performs the Symmetric Difference operation using vectors and comparison logic.


/* C++ Program to find Symmetric Difference of Two Sets */

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    int n, m;

    cout << "Enter number of elements in Set A: ";
    cin >> n;

    vector<int> A(n);

    cout << "Enter elements of Set A:\n";

    for(int i = 0; i < n; i++)
    {
        cin >> A[i];
    }

    cout << "Enter number of elements in Set B: ";
    cin >> m;

    vector<int> B(m);

    cout << "Enter elements of Set B:\n";

    for(int i = 0; i < m; i++)
    {
        cin >> B[i];
    }

    cout << "\nSymmetric Difference of A and B:\n";

    bool foundAny = false;

    /*
       Find A - B
    */

    for(int i = 0; i < n; i++)
    {
        bool found = false;

        for(int j = 0; j < m; j++)
        {
            if(A[i] == B[j])
            {
                found = true;
                break;
            }
        }

        if(!found)
        {
            cout << A[i] << " ";
            foundAny = true;
        }
    }

    /*
       Find B - A
    */

    for(int i = 0; i < m; i++)
    {
        bool found = false;

        for(int j = 0; j < n; j++)
        {
            if(B[i] == A[j])
            {
                found = true;
                break;
            }
        }

        if(!found)
        {
            cout << B[i] << " ";
            foundAny = true;
        }
    }

    if(!foundAny)
    {
        cout << "Empty Set";
    }

    return 0;
}

Sample Output (C++)

Enter number of elements in Set A: 4

Enter elements of Set A:
1 2 3 4

Enter number of elements in Set B: 4

Enter elements of Set B:
3 4 5 6

Symmetric Difference of A and B:
1 2 5 6

Java Program

The following Java program finds the elements that belong to exactly one of the two sets.


/* Java Program to find Symmetric Difference of Two Sets */

import java.util.Scanner;

public class SetSymmetricDifference
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);

        int n, m;

        System.out.print("Enter number of elements in Set A: ");
        n = sc.nextInt();

        int[] A = new int[n];

        System.out.println("Enter elements of Set A:");

        for(int i = 0; i < n; i++)
        {
            A[i] = sc.nextInt();
        }

        System.out.print("Enter number of elements in Set B: ");
        m = sc.nextInt();

        int[] B = new int[m];

        System.out.println("Enter elements of Set B:");

        for(int i = 0; i < m; i++)
        {
            B[i] = sc.nextInt();
        }

        System.out.println("\nSymmetric Difference of A and B:");

        boolean foundAny = false;

        /*
           Find A - B
        */

        for(int i = 0; i < n; i++)
        {
            boolean found = false;

            for(int j = 0; j < m; j++)
            {
                if(A[i] == B[j])
                {
                    found = true;
                    break;
                }
            }

            if(!found)
            {
                System.out.print(A[i] + " ");
                foundAny = true;
            }
        }

        /*
           Find B - A
        */

        for(int i = 0; i < m; i++)
        {
            boolean found = false;

            for(int j = 0; j < n; j++)
            {
                if(B[i] == A[j])
                {
                    found = true;
                    break;
                }
            }

            if(!found)
            {
                System.out.print(B[i] + " ");
                foundAny = true;
            }
        }

        if(!foundAny)
        {
            System.out.print("Empty Set");
        }

        sc.close();
    }
}

Sample Output (Java)

Enter number of elements in Set A: 4

Enter elements of Set A:
1 2 3 4

Enter number of elements in Set B: 4

Enter elements of Set B:
3 4 5 6

Symmetric Difference of A and B:
1 2 5 6

Python Program

Python provides a built-in set data type. The Symmetric Difference can be performed using the ^ operator.


# Python Program to find Symmetric Difference of Two Sets

A = {1, 2, 3, 4}
B = {3, 4, 5, 6}

print("Set A =", A)
print("Set B =", B)

result = A ^ B

print("Symmetric Difference =", result)

Sample Output (Python)

Set A = {1, 2, 3, 4}

Set B = {3, 4, 5, 6}

Symmetric Difference = {1, 2, 5, 6}

Python Using symmetric_difference() Method

Python also provides the symmetric_difference() method.


# Python Program using symmetric_difference() method

A = {1, 2, 3, 4}
B = {3, 4, 5, 6}

result = A.symmetric_difference(B)

print("Set A =", A)
print("Set B =", B)
print("Symmetric Difference =", result)

Python Without Using Built-in Symmetric Difference

The following program implements the operation manually using the concept of Set Difference.


# Symmetric Difference without using built-in functions

A = {1, 2, 3, 4}
B = {3, 4, 5, 6}

result = set()

# Find A - B

for element in A:

    if element not in B:

        result.add(element)

# Find B - A

for element in B:

    if element not in A:

        result.add(element)

print("Set A =", A)
print("Set B =", B)
print("Symmetric Difference =", result)

Symmetric Difference using Union and Intersection

The Symmetric Difference can also be calculated using the formula:

A △ B = (A ∪ B) − (A ∩ B)


# Symmetric Difference using Union and Intersection

A = {1, 2, 3, 4}
B = {3, 4, 5, 6}

union = A | B
intersection = A & B

result = union - intersection

print("Union =", union)
print("Intersection =", intersection)
print("Symmetric Difference =", result)

Dry Run

Consider:

A = {1, 2, 3, 4}

B = {3, 4, 5, 6}

First we find A − B.

Step Element Present in B? Action Result
1 1 No Add 1 {1}
2 2 No Add 2 {1,2}
3 3 Yes Skip 3 {1,2}
4 4 Yes Skip 4 {1,2}

A − B = {1,2}


Finding B − A

Step Element Present in A? Action Result
1 3 Yes Skip 3 {}
2 4 Yes Skip 4 {}
3 5 No Add 5 {5}
4 6 No Add 6 {5,6}

B − A = {5,6}


Final Symmetric Difference

The two difference sets are:

A − B = {1,2}

B − A = {5,6}

Therefore:

A △ B = {1,2,5,6}


Time Complexity

Implementation Time Complexity
C O(n × m)
C++ O(n × m)
Java O(n × m)
Python Manual Method O(n × m)

The nested loops compare elements of one set with elements of the other set. Therefore, the basic array-based implementation has O(n × m) time complexity.


Space Complexity

The result can contain elements from both sets. Therefore, the additional space required for the result can be:

O(n + m)


Properties of Symmetric Difference

  • Symmetric Difference contains elements belonging to exactly one set.
  • Common elements are excluded.
  • It is commutative.
  • It is associative.
  • A △ A = ∅.
  • A △ ∅ = A.
  • A △ B = B △ A.

Important Property

Unlike ordinary Set Difference, Symmetric Difference is commutative.

A △ B = B △ A


Difference Between Set Difference and Symmetric Difference

Operation Meaning Example
A − B Elements in A but not in B {1,2}
B − A Elements in B but not in A {5,6}
A △ B Elements in either A or B but not both {1,2,5,6}

Applications

  • Database Operations
  • Data Comparison
  • Data Analysis
  • Finding Unique Records
  • Database Synchronization
  • Data Cleaning
  • Information Retrieval
  • Comparing User Groups
  • Artificial Intelligence
  • Machine Learning

Advantages

  • Easy to understand and implement.
  • Helps identify elements unique to either set.
  • Useful for comparing two collections.
  • Can be implemented in multiple programming languages.
  • Python provides built-in support for the operation.

Disadvantages

  • Simple array-based implementations require nested loops.
  • Large sets may increase execution time.
  • Additional memory may be required for storing the result.

Viva Questions

  1. What is Symmetric Difference?
  2. What symbol is used for Symmetric Difference?
  3. What is the formula for Symmetric Difference?
  4. What is A △ B?
  5. What is the difference between Difference and Symmetric Difference?
  6. Is Symmetric Difference commutative?
  7. What is A △ A?
  8. What is A △ ∅?
  9. How can Symmetric Difference be implemented in Python?
  10. What is the time complexity of the given algorithm?

Frequently Asked Interview Questions

  1. What is Symmetric Difference?
    It is the set of elements that belong to either of two sets but do not belong to both sets.

  2. How is Symmetric Difference represented mathematically?
    A △ B = (A − B) ∪ (B − A)

  3. Is Symmetric Difference commutative?
    Yes.

    A △ B = B △ A

  4. What is the Symmetric Difference of two identical sets?
    The result is an empty set.

    A △ A = ∅

  5. What is the difference between A − B and A △ B?
    A − B contains only elements belonging to A but not B. A △ B contains elements unique to either A or B.

  6. How is Symmetric Difference performed in Python?
    It can be performed using the ^ operator or the symmetric_difference() method.

Practice Questions

  1. Write a C program to find the Symmetric Difference of two sets.
  2. Write a C++ program to find the Symmetric Difference of two sets.
  3. Write a Java program to find the Symmetric Difference of two sets.
  4. Write a Python program to find the Symmetric Difference of two sets.
  5. Find the Symmetric Difference of {2,4,6} and {4,5,6,7}.
  6. Find A △ B where A = {1,2,3} and B = {3,4,5}.
  7. Implement Symmetric Difference without using built-in functions.
  8. Explain the relationship between Difference and Symmetric Difference.
  9. Prove that A △ B = B △ A.
  10. Find A △ A.

Key Takeaways

  • Symmetric Difference contains elements belonging to exactly one set.
  • Common elements are removed.
  • It is represented by the symbol .
  • A △ B = (A − B) ∪ (B − A).
  • A △ B = (A ∪ B) − (A ∩ B).
  • Symmetric Difference is commutative.
  • A △ A = ∅.
  • A △ ∅ = A.
  • Python provides the ^ operator for Symmetric Difference.

Summary

The Symmetric Difference operation finds the elements that belong to either of two sets but do not belong to both sets. For example, if A = {1,2,3,4} and B = {3,4,5,6}, then A − B = {1,2} and B − A = {5,6}. Therefore, A △ B = {1,2,5,6}. The Symmetric Difference can also be calculated using the formula (A ∪ B) − (A ∩ B). In this experiment, the operation has been implemented using C, C++, Java and Python.


AKTU Examination Tip

Students should be able to:

  • Define Symmetric Difference.
  • Explain the mathematical representation.
  • Write the formula A △ B = (A − B) ∪ (B − A).
  • Explain the difference between Difference and Symmetric Difference.
  • Write the algorithm.
  • Draw the flowchart.
  • Write the C Program.
  • Write the C++ Program.
  • Write the Java Program.
  • Write the Python Program.
  • Perform a dry run.
  • Explain the time complexity.
  • Explain why Symmetric Difference is commutative.






❮ Previous    Next ❯