Advertisement
desislava_topuzakova

2. Sets of Elements

May 25th, 2022
778
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.21 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace _02._Sets_of_Elements
  5. {
  6.     internal class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             //n -> дължина на сет1
  11.             //m -> дължина на сет2
  12.             //"3 4".Split(' ') -> ["3", "4"]
  13.             string input = Console.ReadLine(); //"4 3"
  14.             int n = int.Parse(input.Split(' ')[0]);
  15.             int m = int.Parse(input.Split(' ')[1]);
  16.  
  17.             HashSet<int> firstSet = new HashSet<int>(); //сет 1
  18.             for (int i = 1; i <= n; i++)
  19.             {
  20.                 firstSet.Add(int.Parse(Console.ReadLine()));
  21.             }
  22.  
  23.             HashSet<int> secondSet = new HashSet<int>(); //сет 2
  24.             for (int i = 1; i <= m; i++)
  25.             {
  26.                 secondSet.Add(int.Parse(Console.ReadLine()));
  27.             }
  28.  
  29.             //firstSet -> {1, 3, 5, 7}
  30.             //secondSet -> {3, 4, 5}
  31.             firstSet.IntersectWith(secondSet); //в първия сет остават само елементи, които ги има във втория
  32.             //firstSet -> {3, 5}
  33.             Console.WriteLine(string.Join(" ", firstSet));
  34.         }
  35.     }
  36. }
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement