Skip to content

Instantly share code, notes, and snippets.

@FrankKerrigan
Created February 9, 2019 20:44
Show Gist options
  • Save FrankKerrigan/6bd78ee4620a68824e4e5cd3397ae205 to your computer and use it in GitHub Desktop.
Save FrankKerrigan/6bd78ee4620a68824e4e5cd3397ae205 to your computer and use it in GitHub Desktop.
C# Manual List Union; finds items in both lists and display distinct list
static void Union()
{
int[] first = new[] { 1, 2, 3, 4, 6, 7, 7, 8, 8, 75, 4, 4, 3, 3, 3, 3, 4, 56, 6, 7, 7, 6, 4, 3, 2, 35, 56, 56, 56, 5, 77 };
int[] second = new[] { 1, 3, 5 };
List<int> InBoth = new List<int>();
for(var i = 0; i< first.Length; i++)
{
for(var v =0; v<second.Length; v++)
{
if (first[i] == second[v])
{
bool canAdd = true;
foreach(var z in InBoth)
{
if (z == second[v])
canAdd = false;
if (!canAdd)
break;
}
if (canAdd)
InBoth.Add(first[i]);
}
}
}
foreach(var b in InBoth)
{
Console.Write(b + " ");
}
Console.WriteLine();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment