Advertisement
Torgach

Merging into one collection

Jan 19th, 2023 (edited)
814
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.86 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Collections.ObjectModel;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.IO;
  9.  
  10. namespace Основы.Практика
  11. {
  12.     class Program
  13.     {
  14.         static void Main()
  15.         {
  16.             List<string> collection = new List<string>();
  17.  
  18.             string[] firstArray =
  19.             {
  20.                 "2", "2", "2", "4", "5"
  21.             };
  22.  
  23.             string[] secondArray =
  24.             {
  25.                 "4", "4", "4", "2", "1"
  26.             };
  27.  
  28.             Console.WriteLine("Перед сортировкой:");
  29.             PrintArray(firstArray);
  30.             PrintArray(secondArray);
  31.  
  32.             MergeUniqueArraysValuesIntoCollection(firstArray, secondArray, collection);
  33.  
  34.             Console.WriteLine("\nОбъеденная коллекция:");
  35.  
  36.             foreach (var item in collection)
  37.             {
  38.                 Console.Write(item + " ");
  39.             }
  40.         }
  41.  
  42.         public static void PrintArray(string[] array)
  43.         {
  44.             foreach (var item in array)
  45.             {
  46.                 Console.Write(item + " ");
  47.             }
  48.  
  49.             Console.WriteLine();
  50.         }
  51.  
  52.         public static void MergeUniqueArraysValuesIntoCollection(string[] array1, string[] array2, List<string> collection)
  53.         {
  54.             AddUniqueArrayValuesToCollection(array1, collection);
  55.             AddUniqueArrayValuesToCollection(array2, collection);
  56.         }
  57.  
  58.         public static void AddUniqueArrayValuesToCollection(string[] array, List<string> collection)
  59.         {
  60.             foreach (string item in array)
  61.             {
  62.                 if (collection.Contains(item) == false)
  63.                 {
  64.                     collection.Add(item);
  65.                 }
  66.             }
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement