When I print the return of my array, it doesn't shorten the array size so I end up with three 0's after returning 1,2,3,4,5,6,7 minus duplicates. What would be the easiest way to shorten the array before returning?
class Program
{
static void Main(string[] args)
{
int[] numbers = new int[] { 1, 2, 3, 4, 4, 4, 5, 6, 7, 7 };
List<int> input = new List<int>();
input.Add(1);
input.Add(2);
input.Add(3);
input.Add(4);
input.Add(4);
input.Add(5);
input.Add(5);
input.Add(6);
List<int> result = removeDuplicates(input);
foreach (int i in result)
Console.WriteLine("List:" + i);
int[] intResult = removeDuplicateArray(numbers);
foreach (int i in intResult)
Console.WriteLine("Array:" + i);
}
static int[] removeDuplicateArray(int[] inputList)
{
int[] finalArray = new int[inputList.Length];
int i = 0;
foreach (int currValue in inputList)
{
if (!Contains(finalArray, currValue))
{
finalArray[i] = currValue;
i++;
}
}
return finalArray;
}
static bool Contains(Array list, int comparedValue)
{
foreach (int listValue in list)
{
if (listValue == comparedValue)
{
return true;
}
}
return false;
}
static List<int> removeDuplicates(List<int> inputList)
{
List<int> finalList = new List<int>();
foreach (int currValue in inputList)
{
if (!Contains(finalList, currValue))
{
finalList.Add(currValue);
}
}
return finalList;
}
static bool Contains(List<int> list, int comparedValue)
{
foreach (int listValue in list)
{
if (listValue == comparedValue)
{
return true;
}
}
return false;
}
}

New Topic/Question
Reply




MultiQuote







|