I´m doing a console application that removes an element of an array if a passed number by parameter appears more than one time as element of the array.
I ´ve done the main algorithm but I can´t set null the element. Here is my code:
static int[] RemoveElement(ref int[] array, int num)
{
int i;
for (i = 0; i < array.Length; i++)
{
if (array[i] == num)
{
array[i] = 1; //This is a temporal solution.
}
}
return array;
}
Here is the Main:
static void Main(string[] args)
{
int[] a = {10, 2, 2, 3, 4};
Console.WriteLine("Write a number:");
int b = int.Parse(Console.ReadLine());
int[] arrayInts = RemoveElement(ref a, b);
foreach (var variable in arrayInts)
{
Console.WriteLine(variable);
}
Console.ReadKey();
}
Thanks in advance! :-)
List<int>instead of an array!array.remove(i)intcan not benullreffor how you're using that array. (refis very rarely used in general)