Last active
April 25, 2025 12:27
-
-
Save Strelok78/1a2b428f4b281600a46de7030e12736e to your computer and use it in GitHub Desktop.
Concat two string arrays into one collection without duplicates
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
using System.Collections; | |
using System.Diagnostics; | |
using System.Text; | |
namespace iJuniorPractice; | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
string[] arrayOne = { "1", "2", "4" }; | |
string[] arrayTwo = { "1", "2", "6", "8" }; | |
List<int> result = new List<int>(); | |
StringArrayToIntList(result, arrayOne); | |
StringArrayToIntList(result, arrayTwo); | |
WriteListObjects(result); | |
} | |
static void StringArrayToIntList(List<int> list, string[] array) | |
{ | |
foreach (var value in array) | |
{ | |
if(int.TryParse(value, out int number) && list.Contains(number) == false) | |
list.Add(number); | |
} | |
} | |
static void WriteListObjects(List<int> numbersList) | |
{ | |
foreach (var value in numbersList) | |
{ | |
Console.Write(value); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
solution via HashSet: