Java > Array-2 > zeroFront (CodingBat Solution)

Problem:

Return an array that contains the exact same numbers as the given array, but rearranged so that all the zeros are grouped at the start of the array. The order of the non-zero numbers does not matter. So {1, 0, 0, 1} becomes {0 ,0, 1, 1}. You may modify and return the given array or make a new array.

zeroFront({1, 0, 0, 1}) → {0, 0, 1, 1}
zeroFront({0, 1, 1, 0, 1}) → {0, 0, 1, 1, 1}
zeroFront({1, 0}) → {0, 1}


Solution:

public int[] zeroFront(int[] nums) {
  int count = 0;
  for (int i = 0; i < nums.length; i++) {
    if (nums[i] == 0) {
      nums[i] = nums[count];
      nums[count] = 0;
      count++;
    }
  }
  return nums;
}


14 comments :

  1. public int[] zeroFront(int[] nums)
    {
    //imma dumb fuck so i just rushed and decided to sort the new list back, so
    //to do that we will need to keep record of the new spots
    //also we will check backwards

    int count = nums.length - 1;
    int[] temp = new int[nums.length];

    for (int i = nums.length; i > 0; i--)
    {
    if (nums[i-1] != 0) //so if its not a zero
    {
    temp[count] = nums[i-1];
    //puts it at the last position avaliable
    count--;
    //and takes record of that
    }
    }
    //all the numbers that are not assigned a number are already initialized as 0 so we just

    return temp;
    }

    ReplyDelete
    Replies
    1. // simple logic: count number of 0's in array and put 0 until that position and after that put other digit which is present.

      public int[] zeroFront(int[] nums) {
      int ctr=0; //to find number of 0's
      int num=0; // to store value which is there another than 0
      int len=nums.length; // length of original array

      int ans[]=new int[len]; //new array

      for(int i=0;i<len;i++){
      if(nums[i]==0) //check how many 0 are there
      ctr++;
      else // store the other digit in num
      num=nums[i];
      }

      for(int i=0;i<len;i++){
      if(i<ctr) //until i is below ctr we need 0 in new array
      ans[i]=0;
      else // after that we need the remaining value
      ans[i]=num;
      }
      return ans;

      }

      Delete
  2. public int[] zeroFront(int[] nums) {
    int count=0;
    int index=0;
    int val=0;
    int[] arr=new int[nums.length];
    for (int i = 0; i < nums.length; i++) {
    if(nums[i]==0){
    arr[i]=0;
    index++;
    }
    else { count++; val=nums[i];}

    }
    for (int i = 0; i < count; i++) {
    arr[index]=val;
    index++;
    }
    return arr;
    }

    ReplyDelete
  3. public int[] zeroFront(int[] nums) {
    int nums2[] = new int[nums.length];
    int count = 0;

    for (int i = nums.length - 1; i >= 0; i--)
    {
    if (nums[i] != 0)
    {
    nums2[i + count] = nums[i];
    }
    else
    {
    count++;
    }
    }
    return nums2;
    }

    ReplyDelete
  4. public int[] zeroFront(int[] nums) {
    int nums2[] = new int[nums.length];
    int count = 0;

    for (int i = nums.length - 1; i >= 0; i--)
    {
    if (nums[i] != 0)
    {
    nums2[i + count] = nums[i];
    }
    else
    {
    count++;
    }
    }
    return nums2;
    }

    ReplyDelete
  5. public int[] zeroFront(int[] nums) {
    for (int i = 0; i < nums.length; i++)
    if (nums[i] == 0)
    nums[i] = Integer.MIN_VALUE;

    Arrays.sort(nums);

    for (int i = 0; i < nums.length; i++)
    if (nums[i] == Integer.MIN_VALUE)
    nums[i] = 0;

    return nums;
    }

    ReplyDelete
  6. public boolean either24(int[] nums) {
    int j = 0;

    boolean result = false;

    for(int i = 1; i<nums.length; i++){
    if(nums[j] == 2 && nums[i] == 2) {
    if(result){
    return false;
    } else {
    result = true;
    }
    }
    if(nums[j] == 4 && nums[i] == 4) {
    if(result) {
    return false;
    } else {
    result = true;
    }
    }
    j++;
    }
    return result;
    }

    ReplyDelete
  7. public int[] zeroFront(int[] nums) {
    int current = nums.length-1;

    for(int i = nums.length-1; i>=0; i--){
    if(Math.abs(nums[i])!= 0){
    nums[current] = nums[i];
    current--;
    }
    }
    while (current >= 0) {
    nums[current] = 0;
    current--;
    }
    return nums;
    }

    ReplyDelete
  8. public int[] withoutTen(int[] nums) {
    int j=0;
    int[] arr = new int[nums.length];
    for (int i = 0; i < nums.length; i++) {
    if (nums[i] == 10) {
    continue;
    }
    else
    {
    arr[j]=nums[i];
    j++;
    }
    }
    return arr;
    }

    ReplyDelete
  9. public int[] zeroFront(int[] nums) {

    int countzero = 0;
    int count = 0;
    int notZero = 0;

    for ( int i = 0 ; i < nums.length ; i++ ) {
    if( nums[i] == 0){
    countzero++;
    }else{
    count++;
    notZero = nums[i];
    }
    }
    for ( int j = 0 ; j < nums.length ; j++){
    nums[j] = 0;

    if(j >= countzero){
    nums[j] = notZero;
    }
    }
    return nums;
    }

    ReplyDelete
  10. public int[] zeroFront(int[] nums) {
    //No new array allocation algorithm solution//
    int i = 0;
    for (int j = 1; j < nums.length; j++)
    {
    if (nums[i] == 0)
    {
    i++;
    }
    else if (nums[j] == 0)
    {
    int temp = nums[i];
    nums[i] = nums[j];
    nums[j] = temp;
    i++;
    }
    }
    return nums;
    }

    ReplyDelete
  11. // Easiest solution maybe

    public int[] zeroFront(int[] nums) {
    int zeros = 0;
    int[] arr = new int[nums.length];

    for(int i=0;i<nums.length;i++){
    if(nums[i] ==0) zeros +=1;
    }

    for(int i=0; i<zeros;i++){
    arr[i] = 0;
    }
    for(int i=0;i<nums.length;i++){
    if(nums[i] !=0){
    arr[zeros++] = nums[i];
    }
    }
    return arr;
    }

    ReplyDelete
  12. Not the best solution but it uses nested for loops.

    public int[] zeroFront(int[] nums)
    {
    int temp = 0;
    for (int i = 0; i < nums.length-1; i++)
    {
    for (int j = 0; j < nums.length; j++)
    {
    if (j != 0 && nums[j] == 0)
    {
    temp = nums[j-1];
    nums[j-1] = 0;
    nums[j] = temp;
    }
    }
    }
    return nums;
    }

    ReplyDelete
  13. for(int i = 0; i<nums.length-1; i++) {
    for(int j = 0; j<nums.length-1; j++) {
    if(nums[j] != 0) {
    int tmp = nums[j];
    nums[j] = nums[j+1];
    nums[j+1] = tmp;
    }
    }
    }
    return nums;

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact