Problem Solving: Introduction to Sets
Learn to read sets from a file and to sort and display them.
In this lesson, we’ll work on sets. Sets are a well-defined collection of non-repetitive objects. In our case, we’ll only be looking at the sets of integers.
In this lesson, we’ll learn to:
- Read integer sets from a file
- Sort the integer sets
- Display the sets
So let’s start!
Reading sets from a file
Task: Load the two sets A and B from the file.
The file has the following format:
The two sizes (size of A and size of B) followed by set A and set B entries.
Here is a sample file:
sets.txt
________
5 6
10 2 3 4 5
2 3 4 7 8 10
As we know, a set cannot contain duplicate elements. Therefore, if a number is added twice, that duplicate should not be allowed to enter the set.
So we will have two sets, say A and B, both containing numbers without repetitions. The numbers can be either positive or negative.
Get hands-on with 1200+ tech skills courses.