-
-
Save StrikerXx798/56a95c7991438715a37c64306a27d543 to your computer and use it in GitHub Desktop.
ДЗ: Объединение в одну коллекцию
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Text; | |
namespace ConsoleApp1 | |
{ | |
internal class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.InputEncoding = Encoding.Unicode; | |
Console.OutputEncoding = Encoding.Unicode; | |
List<int> list1 = new List<int>() { 1, 2, 1 }; | |
List<int> list2 = new List<int>() { 3, 2 }; | |
PrintList(list1, "Список 1: "); | |
PrintList(list2, "Список 2: "); | |
List<int> result = MergetLists(list1, list2); | |
PrintList(result, "Результат: "); | |
} | |
static List<int> MergetLists(List<int> list1, List<int> list2) | |
{ | |
List<int> mergedList = new List<int>(); | |
AddToList(list1, ref mergedList); | |
AddToList(list2, ref mergedList); | |
return mergedList; | |
} | |
static void AddToList(List<int> startList, ref List<int> resultList) | |
{ | |
foreach (int item in startList) | |
{ | |
if (resultList.Contains(item) == false) | |
{ | |
resultList.Add(item); | |
} | |
} | |
} | |
static void PrintList(List<int> list, string listName) | |
{ | |
Console.WriteLine(listName); | |
string joinedText = string.Empty; | |
foreach (int number in list) | |
{ | |
joinedText += number.ToString() + " "; | |
} | |
Console.WriteLine(joinedText); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment