4

If I have the next array:

int[] arr = { 123, 243, 0, 0, 123, 0, 0, 0, 123 };

How can I move all the values which are not equal to 0 left as they can so the array will be built like this:

int[] arr = { 123, 243, 123, 123, 0, 0, 0, 0, 0 };

Thanks!

2
  • 7
    Is this homework? Commented Feb 7, 2012 at 15:22
  • I'd perform some kind of bubble-sort. There is no built in call for this, if that's what you're asking. Commented Feb 7, 2012 at 15:23

7 Answers 7

12

How about with LINQ:

var result = arr.Where(x => x != 0).Concat(arr.Where(x => x == 0)).ToArray();

This is quite readable and has linear time complexity. On the other hand, it runs out-of-place and requires two passes over the input.

Sign up to request clarification or add additional context in comments.

Comments

4

OrderBy:

int[] arr = { 123, 243, 0, 0, 123, 0, 0, 0, 123 }.OrderBy(x => x == 0).ToArray();

1 Comment

Another nice one! Order of booleans is false then true, so this does work! +1
3

All the answers so far create a new array. Really you can just move the items up in a single loop and then fill the rest with 0s.

public static void ShiftZerosRight(this int[] arr)
{
    int j = 0;
    while (j < arr.Length && arr[j] != 0)
    {
        j++;
    }
    for (int i = j; i < arr.Length; i++)
    {
        if (arr[i] != 0)
        {
            arr[j++] = arr[i];
        }
    }
    while (j < arr.Length)
    {
        arr[j++] = 0;    
    }
}

Not as elegant as the single line LINQ expressions, but more efficient - this does not create any new objects (and LINQ creates several and the final new array) and this is a single pass through the array. As an extension method the complexity is not seen in the main body where it can be used as:

int arr[] = { ... };
arr.ShiftZerosRight();

1 Comment

this code will not work. this will fail in first step if array has 0 has start arr[j] != 0
2

Perhaps using Linq with:

        int[] arr = { 123, 243, 0, 0, 123, 0, 0, 0, 123 };

        arr = arr.OrderByDescending(a => a > 0).ToArray<int>();

Comments

1

Try this:

arr.OrderBy(x=>x == 0).ToArray();

Comments

0

Make a new array and transfer the values to it.

int[] newArr = new int[arr.Length];
int i = 0;
foreach ( var v in arr )
{
    if (v != 0) 
    {
        newArr[i++] = v;
    }
}
arr = newArr;

Since int is a value type the array is initialized with all zeroes. Then we copy the values one at a time only increasing the destination index i if the value is not 0. More verbose than the Linq examples shown, and decidedly uncooler. But if you're a student it might be easier to follow.

Comments

0

This code snippet doesn't create another array.Here 'x[]' is your array. You take the 1st 0 value and replace it with a non zero number.

int i=0,j=0,index=0,temp=0;
for(i=0;i<x.length;i++)
{
    if(x[i]==0)
    {
       index=i;
       for(j=index;j<x.length;j++)
       {
          if(x[j]!=0)
          {
            temp=x[j];
            x[j]=x[i];
            x[i]=temp;
            break;
          }
       }
    }
}

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.