Skip to content

Instantly share code, notes, and snippets.

@FrankKerrigan
Created February 9, 2019 20:40
Show Gist options
  • Save FrankKerrigan/538e403311d15eab5352d959d9f8e314 to your computer and use it in GitHub Desktop.
Save FrankKerrigan/538e403311d15eab5352d959d9f8e314 to your computer and use it in GitHub Desktop.
C# Distinct on array
// Manual Distinct using C#
// Used List<int> to make it easier to read
static void Distinct()
{
int[] allList = 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 };
List<int> distinct = new List<int>();
foreach(var i in allList)
{
bool canAdd = true;
foreach (var v in distinct)
{
if (v == i)
{
canAdd = false;
}
if (!canAdd)
break;
}
if (canAdd)
distinct.Add(i);
}
foreach (var b in distinct)
{
Console.Write(b + " ");
}
Console.WriteLine();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment