desislava_topuzakova

Обединение и сечение

Jun 6th, 2021
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Demo
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. List<int> firstList = new List<int> { 3, 4, 5, 6 };
  11. List<int> secondsList = new List<int> { 1, 2, 4, 5, 7 };
  12. List<int> union = Union(firstList, secondsList);
  13. List<int> intersect = Intersect(firstList, secondsList);
  14.  
  15.  
  16.  
  17.  
  18.  
  19. }
  20.  
  21. public static List<int> Union( List<int> firstList, List<int> secondList)
  22. {
  23. List<int> union = new List<int>();
  24. union.AddRange(firstList);
  25. foreach (var item in secondList)
  26. {
  27. if (!union.Contains(item))
  28. {
  29. union.Add(item);
  30. }
  31. }
  32. return union;
  33. }
  34.  
  35. public static List<int> Intersect(List<int> firstList, List<int> secondList)
  36. {
  37. List<int> intersect = new List<int>();
  38. foreach (var item in firstList)
  39. {
  40. if (secondList.Contains(item))
  41. {
  42. intersect.Add(item);
  43. }
  44. }
  45. return intersect;
  46. }
  47.  
  48.  
  49. }
  50. }
  51.  
Advertisement
Add Comment
Please, Sign In to add comment