Skip to content

Instantly share code, notes, and snippets.

@Flappizy
Created April 20, 2021 03:34
Show Gist options
  • Save Flappizy/c3cc4d9d8e8dc2f0b331db75f99daffc to your computer and use it in GitHub Desktop.
Save Flappizy/c3cc4d9d8e8dc2f0b331db75f99daffc to your computer and use it in GitHub Desktop.
Algorithm Fridays
class DuplicateRemoval
{
public static int RemoveDuplicate(int[] numbers)
{
if (numbers.Length == 0)
{
return 0;
}
List<int> newArr = new List<int>();
int index = 0;
for (int i = 0; i < numbers.Length; i++)
{
if (newArr.Count == 0)
{
newArr.Add(numbers[i]);
continue;
}
else if (newArr[index] != numbers[i] )
{
newArr.Add(numbers[i]);
index++;
continue;
}
}
//This might be an expensive operation if the set of numbers is really Large, but here i am working with a small set of numbers, so
//it is not a problem
numbers = newArr.ToArray();
return numbers.Length;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment