3

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! :-)

9
  • 2
    If you want to remove elements, use a List<int> instead of an array! Commented Jun 3, 2015 at 17:09
  • You could use an ArrayList instead. Then you could do array.remove(i) Commented Jun 3, 2015 at 17:09
  • 4
    int can not be null Commented Jun 3, 2015 at 17:10
  • 4
    @KevinMee Note: "For a strongly-typed alternative to ArrayList, consider using List<T>" Commented Jun 3, 2015 at 17:11
  • 3
    You don't need ref for how you're using that array. (ref is very rarely used in general) Commented Jun 3, 2015 at 17:12

2 Answers 2

1

I'm not entirely sure what you're after, but let me straighten a few things out. This should solve your issue.

  1. If you want to set an element in an array to null, make sure the element types of the array are a reference type.

For example: int is a value type, and therefore cannot be set to null. You can however use an object, or better for integers: use the nullable-int variant: int?, and set it to null.

In short, this DOES NOT work:

int[] foo = new int[10];
foo[1] = null; // error: int is a value type
  1. If you want to modify an array, do it in a list; even though you can realloc and copy elements of an array, it's error-prone and probably gives you issues.

So, in code:

List<int> foo = new List<int> { 1,2,3,4,5};
foo.RemoveAt(2);
  1. If you remove a lot of elements in the middle of a List, you're probably not looking for a List.

There are data structures way more suitable for 'random removal' if that's what you want. For example, linked lists allow you to remove the middle element without moving all data around, and if you need a 'bag of values', use a HashSet or a Dictionary.

  1. And if you still want an array...

It is of course possible, just probably not very efficient:

int[] array = new int[10]; //...
Array.Copy(array, src, array, src-1, array.Length - src - 1);
Array.Resize(ref array, array.Length-1);
Sign up to request clarification or add additional context in comments.

3 Comments

Note that int? is not a reference type. It's a struct that has special treatment when setting and comparing to null.
Thank you for the explanation! Thank you also all the people that commented :D
@juharr Yes, that's why I mention object, which boxes the value type. You're right, it's a good addition.
0

You can use LINQ to remove repeated elements:

var intArray = new[] { 10, 2, 2, 3, 4 };
var remove = intArray.Distinct().ToArray();

Also as others have stated there is no NULL int.

Likewise you can try the same using List

List<Type> list1 = list.Distinct().ToList<Type>();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.