0

This is my attempt at this problem, how would I move the 0's to the end of the array? I tried swapping the 0 elements with the end elements and well that wasn't it...

public void removeMiddle()  {
    int pos = values.length/2;
    int n = values.length
    if (n%2 == 0)  {
        int fMid = pos - 1;
        values[pos] = 0;
        values fMid = 0;
    } else  {
        int j = n-1;
        int k = j/2;
        int l = k+1;
        int m = n-l;
        values[m] = 0;
    }
}

Array  =  {5, 2, 7, 9, 1, 3, 2, 4}
result =  [5, 2, 7, 0, 0, 3, 2, 4]
Expected: [5, 2, 7, 3, 2, 4, 0, 0]

Array  =  {5, 2, 7, 9, 1, 3, 2}
result =  [5, 2, 7, 0, 1, 3, 2]
Expected: [5, 2, 7, 1, 3, 2, 0]
4
  • If order of other elements is not important, then convert array to collection and sort in descending order. Commented Apr 8, 2013 at 8:11
  • Where you doing swapping? Commented Apr 8, 2013 at 8:13
  • That was before, but I removed it. Commented Apr 8, 2013 at 8:20
  • @qvd You want to move 0 to the end of array if it contains it ...yes? Commented Apr 8, 2013 at 8:23

5 Answers 5

1

Hint: use System.arraycopy() followed by setting the last element to zero.

Alternatively, use a loop.

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

Comments

1

Convert array to List, remove zeroes, add them back, and convert list back to array.

4 Comments

How do you remove an element from a list. values.remove?
Yes, remove the elements, and add them back and they will get added to the bottom of the list.
I tried values.remove(position) and it said it couldn't find the variable even after I import java.util.List;
You can use value to remove the element.
0

As said by someone earlier you can Convert array to List, remove zeroes, add them back, and convert list back to array or if you want it from scratch and you know the length of the array then just make one array (say array1) and scan the original array (say array) till the end of the array. If the array contains non-zero number just insert it in the array(array1). Once you are done with scanning just add zeros to the remaining array index.

Hope this might help you.

Comments

0

Try this: I think its easy to understand and implement.

public static void main(String[] args) {
    int[] arr = { 5, 0, 7, 9, 1, 3, 0 };
    int index = 0;
    for (int i = 0; i < arr.length; i++) {
        if (arr[i] != 0) {
            arr[index++] = arr[i];
        }

    }

    Arrays.fill(arr, index, arr.length, 0);
    System.out.println(Arrays.toString(arr));
}

Input:

{ 5, 0, 7, 9, 1, 3, 0 }

Output:

[5, 7, 9, 1, 3, 0, 0]

Comments

0

maybe you can use the LinkedList structure and add all non 0 elements of your array at the start of it and all 0's at the end.

    //initial array
    Integer[] array ={1,2,5,8,0,9,10};

    //converted array to List
    ArrayList<Integer> inputList= new ArrayList<Integer>(Arrays.asList(array));

    //secondary list 
    LinkedList<Integer> outputList = new <Integer>LinkedList();


    for(Integer x:inputList){
        int val= Integer.parseInt(x.toString());
        if(val !=0){
            //add non 0 values at the start
            outputList.addFirst(x);
        }else{
            //add 0's at the end
            outputList.addLast(x);
        }
    }

    //convert back to array
    Integer[] outputArray = new Integer[outputList.size()];
    outputArray = outputList.toArray(outputArray);

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.