Last active
May 17, 2023 07:41
-
-
Save Strelok78/05156db8882045735db6183f092e0bd8 to your computer and use it in GitHub Desktop.
Combining strings arrays without using LINQ and avoiding repeats
This file contains hidden or 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
//Есть два массива строк. Надо их объединить в одну коллекцию, | |
//исключив повторения, не используя Linq. Пример: {"1", "2", "1"} + {"3", "2"} => {"1", "2", "3"} | |
internal class Program | |
{ | |
public static void Main() | |
{ | |
string[] arrayNumbers = { "1", "2", "3", "4", "5", "6", "8", "8", "2" }; | |
string[] arrayOddNumbers = { "1", "3", "5", "7", "9" }; | |
List<string> allNumbersList = new List<string>(); | |
MergeArrays(ref allNumbersList, arrayNumbers); | |
MergeArrays(ref allNumbersList, arrayOddNumbers); | |
allNumbersList.Sort(); | |
foreach(string number in allNumbersList) | |
{ | |
Console.WriteLine(number); | |
} | |
} | |
static void MergeArrays(ref List<string> listNumbers, string[] array) | |
{ | |
for (int i = 0; i < array.Length; i++) | |
{ | |
if (listNumbers.Count == 0) | |
listNumbers.Add(array[i]); | |
if (listNumbers.Contains(array[i]) == false) | |
listNumbers.Add(array[i]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment