
Practice 50 Sets Coding Questions
Q1. Write a Rust program to create a set and insert elements into it.
Input:
Elements: 1, 2, 3
Expected Output:
Set: {1, 2, 3}
Q2. Write a Rust program to check if an element exists in a set.
Input:
Set: {1, 2, 3}, Element: 2
Expected Output:
Element 2 is present in the set.
Q3. Write a Rust program to remove an element from a set.
Input:
Set: {1, 2, 3}, Element to remove: 2
Expected Output:
Set after removal: {1, 3}
Q4. Write a Rust program to iterate over the elements of a set.
Input:
Set: {1, 2, 3}
Expected Output:
1
2
3
Q5. Write a Rust program to find the size of a set.
Input:
Set: {1, 2, 3}
Expected Output:
Size of the set: 3
Q6. Write a Rust program to create a set from a vector.
Input:
Vector: [1, 2, 3, 1, 2]
Expected Output:
Set: {1, 2, 3}
Q7. Write a Rust program to check if a set is empty.
Input:
Set: {}
Expected Output:
The set is empty.
Q8. Write a Rust program to clear all elements from a set.
Input:
Set: {1, 2, 3}
Expected Output:
Set after clearing: {}
Q9. Write a Rust program to find the union of two sets.
Input:
Set 1: {1, 2, 3}, Set 2: {3, 4, 5}
Expected Output:
Union: {1, 2, 3, 4, 5}
Q10. Write a Rust program to find the intersection of two sets.
Input:
Set 1: {1, 2, 3}, Set 2: {3, 4, 5}
Expected Output:
Intersection: {3}
Q11. Write a Rust program to find the difference of two sets (elements in set 1 but not in set 2).
Input:
Set 1: {1, 2, 3}, Set 2: {3, 4, 5}
Expected Output:
Difference: {1, 2}
Q12. Write a Rust program to check if one set is a subset of another.
Input:
Set 1: {1, 2}, Set 2: {1, 2, 3}
Expected Output:
Set 1 is a subset of Set 2.
Q13. Write a Rust program to check if two sets are equal.
Input:
Set 1: {1, 2, 3}, Set 2: {1, 2, 3}
Expected Output:
The sets are equal.
Q14. Write a Rust program to convert a set into a vector.
Input:
Set: {1, 2, 3}
Expected Output:
Vector: [1, 2, 3]
Q15. Write a Rust program to add multiple elements to a set at once.
Input:
Set: {1, 2}, Elements to add: 3, 4, 5
Expected Output:
Set after adding elements: {1, 2, 3, 4, 5}
Q16. Write a Rust program to remove multiple elements from a set.
Input:
Set: {1, 2, 3, 4}, Elements to remove: 2, 4
Expected Output:
Set after removal: {1, 3}
Q17. Write a Rust program to check if a set contains all the elements of another set.
Input:
Set 1: {1, 2, 3}, Set 2: {2, 3}
Expected Output:
Set 1 contains all elements of Set 2.
Q18. Write a Rust program to convert a set of strings to uppercase.
Input:
Set: {"rust", "is", "fun"}
Expected Output:
Set in uppercase: {"RUST", "IS", "FUN"}
Q19. Write a Rust program to find the symmetric difference of two sets.
Input:
Set 1: {1, 2, 3}, Set 2: {3, 4, 5}
Expected Output:
Symmetric difference: {1, 2, 4, 5}
Q20. Write a Rust program to create a set and check if it contains any specific element.
Input:
Set: {1, 2, 3}, Element to check: 2
Expected Output:
Set contains the element 2.
Q21. Write a Rust program to find the maximum element in a set of integers.
Input:
Set: {1, 2, 3, 4, 5}
Expected Output:
Maximum element: 5
Q22. Write a Rust program to find the minimum element in a set of integers.
Input:
Set: {1, 2, 3, 4, 5}
Expected Output:
Minimum element: 1
Q23. Write a Rust program to check if a set contains only even numbers.
Input:
Set: {2, 4, 6, 8}
Expected Output:
The set contains only even numbers.
Q24. Write a Rust program to merge two sets.
Input:
Set 1: {1, 2, 3}, Set 2: {3, 4, 5}
Expected Output:
Merged Set: {1, 2, 3, 4, 5}
Q25. Write a Rust program to find the difference between two sets where elements in set 2 but not in set 1.
Input:
Set 1: {1, 2, 3}, Set 2: {3, 4, 5}
Expected Output:
Difference (Set 2 - Set 1): {4, 5}
Q26. Write a Rust program to create a set of characters from a string.
Input:
String: "hello"
Expected Output:
Set of characters: {'h', 'e', 'l', 'o'}
Q27. Write a Rust program to check if two sets are disjoint (no common elements).
Input:
Set 1: {1, 2, 3}, Set 2: {4, 5, 6}
Expected Output:
The sets are disjoint.
Q28. Write a Rust program to find the average of all elements in a set of integers.
Input:
Set: {1, 2, 3, 4}
Expected Output:
Average: 2.5
Q29. Write a Rust program to create a set of squares of integers.
Input:
Integers: [1, 2, 3, 4]
Expected Output:
Set of squares: {1, 4, 9, 16}
Q30. Write a Rust program to convert a set of integers into a set of strings.
Input:
Set: {1, 2, 3}
Expected Output:
Set of strings: {"1", "2", "3"}
Q31. Write a Rust program to remove duplicate elements from a vector using a set.
Input:
Vector: [1, 2, 2, 3, 3, 4]
Expected Output:
Set after removing duplicates: {1, 2, 3, 4}
Q32. Write a Rust program to create a set of odd numbers from a list of integers.
Input:
List: [1, 2, 3, 4, 5]
Expected Output:
Set of odd numbers: {1, 3, 5}
Q33. Write a Rust program to find the union of three sets.
Input:
Set 1: {1, 2}, Set 2: {2, 3}, Set 3: {3, 4}
Expected Output:
Union of three sets: {1, 2, 3, 4}
Q34. Write a Rust program to count the number of elements in a set.
Input:
Set: {1, 2, 3, 4}
Expected Output:
Number of elements: 4
Q35. Write a Rust program to check if an element is not present in a set.
Input:
Set: {1, 2, 3}, Element: 4
Expected Output:
Element 4 is not present in the set.
Q36. Write a Rust program to find the sum of elements in a set of integers.
Input:
Set: {1, 2, 3}
Expected Output:
Sum: 6
Q37. Write a Rust program to create a set from two vectors and merge them.
Input:
Vector 1: [1, 2], Vector 2: [2, 3]
Expected Output:
Merged Set: {1, 2, 3}
Q38. Write a Rust program to create a set of even numbers from a range.
Input:
Range: 1 to 10
Expected Output:
Set of even numbers: {2, 4, 6, 8, 10}
Q39. Write a Rust program to create a set and check if it contains any negative numbers.
Input:
Set: {-1, 2, 3, -4}
Expected Output:
The set contains negative numbers.
Q40. Write a Rust program to find the smallest element in a set.
Input:
Set: {5, 3, 8, 2}
Expected Output:
Smallest element: 2
Q41. Write a Rust program to perform a set operation that adds the difference of two sets.
Input:
Set 1: {1, 2, 3}, Set 2: {3, 4, 5}
Expected Output:
Set after difference operation: {1, 2}
Q42. Write a Rust program to create a set of characters that appear more than once in a string.
Input:
String: "programming"
Expected Output:
Set of characters appearing more than once: {'r', 'g'}
Q43. Write a Rust program to generate a set of Fibonacci numbers up to a certain limit.
Input:
Limit: 10
Expected Output:
Set of Fibonacci numbers: {1, 2, 3, 5, 8}
Q44. Write a Rust program to check if two sets are identical.
Input:
Set 1: {1, 2, 3}, Set 2: {1, 2, 3}
Expected Output:
The sets are identical.
Q45. Write a Rust program to remove all elements greater than a certain threshold from a set.
Input:
Set: {1, 2, 3, 4, 5}, Threshold: 3
Expected Output:
Set after removal: {1, 2, 3}
Q46. Write a Rust program to combine two sets and sort the elements in ascending order.
Input:
Set 1: {3, 1, 2}, Set 2: {5, 4, 6}
Expected Output:
Sorted set: {1, 2, 3, 4, 5, 6}
Q47. Write a Rust program to check if a set contains no negative numbers.
Input:
Set: {1, 2, 3}
Expected Output:
The set contains no negative numbers.
Q48. Write a Rust program to merge multiple sets.
Input:
Set 1: {1, 2}, Set 2: {2, 3}, Set 3: {3, 4}
Expected Output:
Merged Set: {1, 2, 3, 4}
Q49. Write a Rust program to create a set and check if it is a superset of another set.
Input:
Set 1: {1, 2, 3}, Set 2: {1, 2}
Expected Output:
Set 1 is a superset of Set 2.
Q50. Write a Rust program to find the intersection of three sets.
Input:
Set 1: {1, 2, 3}, Set 2: {3, 4, 5}, Set 3: {3, 6, 7}
Expected Output:
Intersection of three sets: {3}