W1thr

Коллекции-5

Jun 11th, 2022 (edited)
1,295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.14 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Homework5
  5. {
  6.     internal class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             string[] strings1 = { "1", "2", "1" };
  11.             string[] strings2 = { "3", "2" };
  12.  
  13.             List<string> resultingList = new List<string>();
  14.  
  15.             resultingList.Add(strings1[0]);
  16.  
  17.             TryAddElement(strings1, resultingList);
  18.             TryAddElement(strings2, resultingList);
  19.  
  20.             for (int i = 0; i < resultingList.Count; i++)
  21.                 Console.WriteLine(resultingList[i]);
  22.         }
  23.  
  24.         private static void TryAddElement(string[] strings, List<string> resultingList)
  25.         {
  26.             for (int i = 0; i < strings.Length; i++)
  27.             {
  28.                 bool alreadyExists = false;
  29.                 for (int j = 0; j < resultingList.Count; j++)
  30.                 {
  31.                     if (strings[i] == resultingList[j])
  32.                         alreadyExists = true;
  33.                 }
  34.  
  35.                 if (alreadyExists == false)
  36.                     resultingList.Add(strings[i]);
  37.             }
  38.         }
  39.     }
  40. }
  41.  
Add Comment
Please, Sign In to add comment