illiden

UnionCollections

Jul 13th, 2022 (edited)
672
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace CSLight
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             string[] firstArray = InputStringArray(' ');
  11.             string[] secondArray = InputStringArray(' ');
  12.             List<string> result = UnionArrays(firstArray, secondArray);
  13.             Console.WriteLine("Итоговая коллекция: ");
  14.             PrintCollection(result);
  15.         }
  16.  
  17.         private static void PrintCollection<T>(IList<T> collection)
  18.         {
  19.             foreach (T element in collection)
  20.             {
  21.                 Console.Write($"{element} ");
  22.             }
  23.             Console.WriteLine();
  24.         }
  25.  
  26.         private static List<T> UnionArrays<T>(IList<T> firstCollection, IList<T> secondCollection)
  27.         {
  28.             List<T> unionCollection = new List<T>(firstCollection);
  29.             unionCollection.AddRange(secondCollection);
  30.             return Distinct(unionCollection);
  31.         }
  32.  
  33.         private static List<T> Distinct<T>(IList<T> collection)
  34.         {
  35.             List<T> distinctCollection = new List<T>();
  36.             foreach (T element in collection)
  37.             {
  38.                 if(distinctCollection.Contains(element) == false)
  39.                     distinctCollection.Add(element);
  40.             }
  41.  
  42.             return distinctCollection;
  43.         }
  44.  
  45.         private static string[] InputStringArray(char separator)
  46.         {
  47.             Console.WriteLine($"Введите массив через '{separator}'");
  48.             string text = Console.ReadLine();
  49.             string[] result = text.Split(separator);
  50.             return result;
  51.         }
  52.     }
  53. }
Add Comment
Please, Sign In to add comment