Created
April 20, 2021 03:34
-
-
Save Flappizy/c3cc4d9d8e8dc2f0b331db75f99daffc to your computer and use it in GitHub Desktop.
Algorithm Fridays
This file contains 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
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