Question #269364

Write a program that involving 2 hash sets populate with 5 members coming from the user. Perform the set operation (UNION, INTERSECTION)


Expert's answer

 internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Input values for first hash set: ");
            Input(out HashSet<int> firstHashSet);
            Console.WriteLine("Input values for second hash set: ");
            Input(out HashSet<int> secondHashSet);
            Console.WriteLine($"UNION result: {string.Join(" ", firstHashSet.Union(secondHashSet).ToArray())}");
            Console.WriteLine($"INTERSECTION result: {string.Join(" ", firstHashSet.Intersect(secondHashSet).ToArray())}");


            Console.ReadLine();
        }
        public static void Input(out HashSet<int> collection)
        {
            collection = new HashSet<int>();
            for (int i = 0; i < 5; i++)
            {
                Console.Write($"{i + 1}. = ");
                collection.Add(int.Parse(Console.ReadLine()));
            }
        }
    }
LATEST TUTORIALS
APPROVED BY CLIENTS