Created
November 19, 2015 19:47
-
-
Save ivankahl/942a08f5321a99950ade to your computer and use it in GitHub Desktop.
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
static int[] RemoveDuplicates(int[] items) | |
{ | |
// Create a new array to store the items with no | |
// duplicates | |
List<int> newList = new List<int> { items[0] }; | |
// Loop through every item in our items array | |
for (int i = 1; i < items.Length; i++) | |
{ | |
// Create a new boolean to determine whether | |
// the item exists in the newList or not | |
bool inList = false; | |
// Create a counter to loop through newList | |
int j = 0; | |
// Loop through newList until we reach the | |
// end or until we find the item in the list | |
while (j < newList.Count && !inList) | |
{ | |
// Check if the current item in newList | |
// matches the current item in our items | |
// array | |
if (items[i] == newList[j]) | |
// If it does, we know that the item | |
// is already in newList and we do not | |
// want to add it again | |
inList = true; | |
j++; | |
} | |
// Check if the item is in the list or not | |
if (!inList) | |
// If it is not, add it | |
newList.Add(items[i]); | |
} | |
// Return the newList as an array | |
return newList.ToArray(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment