Union of Two Sets using C and Python
Objective
To write a program in C and Python to create two sets and perform the Union operation on them.
Prerequisites
- Basic knowledge of Set Theory.
- Knowledge of Arrays.
- Basic understanding of C Programming.
- Basic understanding of Python Programming.
Introduction
A Set is a collection of unique elements. The Union operation combines all distinct elements from two sets. If an element exists in both sets, it is stored only once in the Union Set.
B = {3,4,5,6}
A ∪ B = {1,2,3,4,5,6}
The mathematical symbol used for Union is ∪.
Mathematical Representation
The Union contains every element that belongs to Set A, Set B or both.
Example
| Set A | Set B | Union (A ∪ B) |
|---|---|---|
| {1,2,3,4} | {3,4,5,6} | {1,2,3,4,5,6} |
| {10,20} | {20,30,40} | {10,20,30,40} |
| {5,6} | {7,8} | {5,6,7,8} |
- A Set never stores duplicate elements.
- The Union operation automatically removes duplicate values.
- The order of elements is not important in a Set.
Algorithm
- Start.
- Input the number of elements of Set A.
- Read all the elements of Set A.
- Input the number of elements of Set B.
- Read all the elements of Set B.
- Copy every element of Set A into the Result Set.
- Compare each element of Set B with the Result Set.
- If the element is not already present, insert it into the Result Set.
- Repeat until all elements of Set B have been processed.
- Display the Union Set.
- Stop.
Program Logic
The program first stores all the elements of the first set. Next, it checks every element of the second set. If an element is not already available in the Result Set, it is inserted. Finally, the program displays all the unique elements present in both sets.
C Program
#include<stdio.h>
int main()
{
int setA[20], setB[20], unionSet[40];
int m, n;
int i, j;
int k = 0;
int found;
/* Read Set A */
printf("Enter number of elements in Set A : ");
scanf("%d", &m);
printf("\nEnter elements of Set A\n");
for(i = 0; i < m; i++)
{
scanf("%d", &setA[i]);
unionSet[k++] = setA[i];
}
/* Read Set B */
printf("\nEnter number of elements in Set B : ");
scanf("%d", &n);
printf("\nEnter elements of Set B\n");
for(i = 0; i < n; i++)
{
scanf("%d", &setB[i]);
}
/* Perform Union */
for(i = 0; i < n; i++)
{
found = 0;
for(j = 0; j < k; j++)
{
if(setB[i] == unionSet[j])
{
found = 1;
break;
}
}
if(found == 0)
{
unionSet[k++] = setB[i];
}
}
/* Display Result */
printf("\nUnion of the Two Sets is : ");
for(i = 0; i < k; i++)
{
printf("%d ", unionSet[i]);
}
printf("\n");
return 0;
}
The program first reads all the elements of Set A and stores them in the Result Set. After that, every element of Set B is compared with the elements already present in the Result Set. If the element is not found, it is inserted into the Result Set. Finally, the Result Set is displayed, which represents the Union of the two sets.
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 Union of the Two Sets is : 1 2 3 4 5 6
# Program to perform Union of Two Sets
# Read Set A
setA = set()
n = int(input("Enter number of elements in Set A : "))
print("Enter elements of Set A")
for i in range(n):
value = int(input())
setA.add(value)
# Read Set B
setB = set()
m = int(input("\nEnter number of elements in Set B : "))
print("Enter elements of Set B")
for i in range(m):
value = int(input())
setB.add(value)
# Perform Union
unionSet = setA.union(setB)
# Display Result
print("\nSet A =", setA)
print("Set B =", setB)
print("Union =", unionSet)
The Python program first creates two empty sets. The user enters the elements of both sets. Python provides a built-in union() method that automatically combines both sets and removes duplicate elements. Finally, the resulting Union Set is displayed.
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
Set A = {1, 2, 3, 4}
Set B = {3, 4, 5, 6}
Union = {1, 2, 3, 4, 5, 6}
Dry Run
Suppose the user enters the following sets.
| Set A | Set B |
|---|---|
| {1,2,3,4} | {3,4,5,6} |
| Step | Operation | Current Result |
|---|---|---|
| 1 | Read Set A | {1,2,3,4} |
| 2 | Read Set B | {3,4,5,6} |
| 3 | Copy all elements of Set A | {1,2,3,4} |
| 4 | Compare element 3 | Already Exists |
| 5 | Compare element 4 | Already Exists |
| 6 | Insert element 5 | {1,2,3,4,5} |
| 7 | Insert element 6 | {1,2,3,4,5,6} |
| 8 | Display Union Set | {1,2,3,4,5,6} |
Time Complexity
| Implementation | Time Complexity | Description |
|---|---|---|
| C Program | O(m × n) | Each element of Set B is compared with the elements already present in the Union Set. |
| Python Program | O(m + n) | Python uses an optimized hash-based implementation for set operations. |
Space Complexity
| Implementation | Space Complexity |
|---|---|
| C Program | O(m + n) |
| Python Program | O(m + n) |
Working of the Program
The program first stores all the elements of the first set. It then compares every element of the second set with the Result Set. If the element is not already present, it is added to the Result Set. After all comparisons are completed, the final Union Set containing only unique elements is displayed.
Applications of Union of Sets
- Database management systems for combining records from multiple tables.
- Data analysis and data mining applications.
- Social networking applications to combine friend lists.
- Search engines for merging search results.
- Library management systems.
- Student information management systems.
- Artificial Intelligence and Machine Learning for combining datasets.
- Compiler design and formal language processing.
Advantages
- Simple and easy to understand.
- Eliminates duplicate elements automatically.
- Provides a complete collection of unique elements.
- Useful in mathematics as well as computer science.
- Python provides a built-in union() function, making implementation very easy.
Disadvantages
- The C implementation requires nested loops, increasing execution time for large datasets.
- Extra memory is required to store the resulting union set.
- For very large datasets, manual duplicate checking becomes less efficient.
Comparison of C and Python Implementations
| Feature | C Program | Python Program |
|---|---|---|
| Implementation | Uses arrays and loops. | Uses built-in set data structure. |
| Duplicate Removal | Performed manually. | Performed automatically. |
| Programming Effort | More | Less |
| Execution Speed | Generally faster for small programs. | Very efficient because of optimized hash tables. |
| Code Length | Longer | Shorter and easier to read. |
Frequently Asked Viva Questions
- What is a Set?
- What is the Union of two Sets?
- Which symbol represents Union?
- Can duplicate elements exist in a Set?
- Why is Union useful in computer science?
- How is Union implemented in C?
- Which Python function performs the Union operation?
- What is the time complexity of the C implementation?
- What is the difference between Union and Intersection?
- Give two real-life applications of Union.
Interview Questions
- Write a C program to perform the Union of two Sets.
- Write a Python program using the union() method.
- How can duplicate elements be removed without using the set data type?
- Compare Union and Symmetric Difference.
- Explain the time complexity of the Union algorithm.
- Can Union be performed on more than two Sets? Explain.
- What are the advantages of Python's built-in Set data structure?
- Explain the difference between mathematical Sets and arrays.
Practice Questions
- Write a program to find the Union of three Sets.
- Modify the C program to accept character Sets.
- Implement the Union operation without using an additional array.
- Write a menu-driven program to perform all Set operations.
- Write a Python program to find the Union of multiple Sets entered by the user.
Key Takeaways
- Union combines all unique elements of two or more Sets.
- Duplicate elements are included only once.
- The mathematical symbol for Union is ∪.
- C implementation requires manual duplicate checking.
- Python provides the built-in union() method for efficient implementation.
- Union is widely used in databases, data science, artificial intelligence, networking, and software development.
Summary
In this experiment, we studied the Union operation on Sets and implemented it using both C and Python programming languages. We learned the mathematical definition of Union, understood its algorithm, analyzed the program logic, examined sample outputs, discussed time and space complexity, explored real-world applications, and practiced viva as well as interview questions. This experiment forms the foundation for learning other Set operations such as Intersection, Difference, and Symmetric Difference.
AKTU Examination Tip
For university practical examinations, students should be able to explain the Union operation mathematically, write the complete C program without syntax errors, demonstrate the Python implementation using the union() method, and analyze the time complexity of both approaches. Examiners frequently ask students to compare Union with Intersection and Difference, so understanding these concepts thoroughly is recommended.