3

In C#, is it possible to remove an element from an array in place?

Of course, there are a ton of questions on SO about removing items from an array. Every answer either uses a List or creates another array with the new values. However, I'd like to know if it's even possible to modify an array in place (deleting or adding elements) in C#.

In the easy LeetCode question "Remove Duplicates from Sorted Array", you are restricted to the same array, you cannot create a new array in memory (O(1) space in memory). Is this possible using C#? I can't come up with a solution that does not create another array.

4
  • 2
    No, what you are looking for is a linked list. Commented Apr 26, 2018 at 2:18
  • I didn't think it was possible. Of course, normally I would use a List for this type of operation, but I meant specifically for the case of this LeetCode question - which only gives you an int array to work with. Commented Apr 26, 2018 at 2:27
  • of course array can be modified in place .. just changing its Length doesn't seem possible with safe code. List uses array internally, and doesn't create new array when removing items referencesource.microsoft.com/#mscorlib/system/collections/… Commented Apr 26, 2018 at 2:53
  • the examples actually confirm that changing the array length is not needed "It doesn't matter what values are set beyond the returned length." Commented Apr 26, 2018 at 3:03

2 Answers 2

4

In C#, is it possible to remove an element from an array in place?

No, by definition of array. Arrays are fixed-length. You can, however, track the number of "elements you care about" within the array (an integer) and remove elements by copying following elements down to their prior index & decrementing this counter.

You'd then have derived an Array List (List<T> in .NET).

In the easy LeetCode question "Remove Duplicates from Sorted Array", you are restricted to the same array, you cannot create a new array in memory (O(1) space in memory). Is this possible using C#? I can't come up with a solution that does not create another array.

The question wants you to modify the provided array with your solution, then return the length of the subset of the array (starting at index 0) filled with unique values.

A solution in Java is available here: https://leetcode.com/problems/remove-duplicates-from-sorted-array/solution/ - you can change two letters (length to Length twice) to get it compiling in C#.

I'll paste that here anyway:

int RemoveDuplicates(int[] nums) {
    if (nums.Length == 0) return 0;
    int i = 0;
    for (int j = 1; j < nums.Length; j++) {
        if (nums[j] != nums[i]) {
            i++;
            nums[i] = nums[j];
        }
    }
    return i + 1;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Apparently Wikipedia is having an edit war over whether "Array Lists" (aka Dynamic Arrays) are Arrays. I've not heard the term Dynamic Array since the early 2000's - they really are conceptually different (one is used to build the other) and should be thought of as such IMO.
I didn't actually think about not needing to return the array, and only returning the length of the subset. I got stuck on the actual possibility of this in C# rather than the real meat of the question.
Gotcha. Leetcode and other "learn to code with funny problems" sites with user-submitted questions can have really questionable content. Good for beginner algorithmic practice. Bad if you're expecting to write quality code :P
0

Here is a sample of using two arrays and in-place changes:

public static int[] MoveZeros(int[] arr){
    //int[] nonZeros = new int[arr.Length];
    int forwardArrow = 0;
    for(int i=0; i< arr.Length; i++){
      if(arr[i] != 0){
        //Two arrays approach
        //nonZeros[forwardSecoundArrow] = arr[i];
        //forwardSecoundArrow++;
                
        //in-place approach
        var temp = arr[i];
        arr[i] = arr[forwardArrow];
        arr[forwardArrow] = temp;
        forwardArrow++;
      }
    }
    
     //Two arrays approach
    /*
      for(int i=0; i < nonZeros.Length; i++){
        arr[i] = nonZeros[i];
        }
    */
    
    return arr;
  }

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.