Java > Array-2 > shiftLeft (CodingBat Solution)

Problem:

Return an array that is "left shifted" by one -- so {6, 2, 5, 3} returns {2, 5, 3, 6}. You may modify and return the given array, or return a new array.

shiftLeft({6, 2, 5, 3}) → {2, 5, 3, 6}
shiftLeft({1, 2}) → {2, 1}
shiftLeft({1}) → {1}


Solution:

public int[] shiftLeft(int[] nums) {  
  int[] foo;
  foo = new int[nums.length];
  
  if (nums.length == 0)
    return foo;
  
  for (int i = 0; i < nums.length-1; i++) {
    if (i > 0)
      foo[i] = nums[i+1];
  }
  if (nums.length > 1)
    foo[0] = nums[1];
  foo[nums.length-1] = nums[0];
  return foo;
}


12 comments :

  1. public int[] shiftLeft(int[] nums) {
    int len = nums.length;
    int temp = 0;
    for(int i=0; i<len-1; i++){
    temp = nums[i];
    nums[i]= nums[i+1];
    nums[i+1] = temp;
    }
    return nums;
    }

    ReplyDelete
  2. public int[] shiftLeft(int[] nums) {
    int[] newArray;
    int lastNum;

    if(nums.length == 0) {
    return nums;
    } else {
    newArray = nums;
    lastNum = nums[0];

    for(int i = 0; i < nums.length - 1; i++) {
    newArray[i] = nums[i + 1];
    }
    newArray[newArray.length -1] = lastNum;
    }
    return newArray;
    }

    ReplyDelete
  3. public int[] shiftLeft(int[] nums) {
    if (nums.length <= 0) {
    return nums;
    }

    int[] array = new int[nums.length];

    int first = nums[0];

    for (int i = 0, j = 1; j < nums.length; i++, j++) {

    array[i] = nums[j];

    }
    array[array.length - 1] = first;

    return array;

    ReplyDelete
  4. public int[] shiftLeft(int[] nums) {
    if(nums.length <= 1) return nums;

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

    ReplyDelete
  5. public int[] shiftLeft(int[] nums)
    {
    int i,j;
    //int k=nums[0];
    int l=nums.length;
    int a[]=new int[l];
    if(l==0)
    {
    return a;
    }
    int k=nums[0];
    for(i=0;i<1;i++)
    {
    for(j=0;j<l-1;j++)
    {
    a[j]= nums[j+1];
    }
    a[j]=k;
    }
    return a;
    }

    ReplyDelete
  6. public int[] shiftLeft(int[] nums) {
    int temp=0;

    if(nums.length<1)
    return nums;

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

    nums[nums.length-1]=temp;

    return nums;
    }

    ReplyDelete
  7. public int[] shiftLeft(int[] nums) {
    for (int i = 0; i < nums.length - 1; i++)
    {
    int temp = nums[i];
    nums[i] = nums[i + 1];
    nums[i + 1] = temp;
    }
    return nums;
    }

    ReplyDelete
  8. public int[] shiftLeft(int[] nums) {
    if(nums.length<=1){
    return nums;
    }
    int[] newNums = new int[nums.length];
    newNums[nums.length-1]=nums[0];
    for(int i=0; i<nums.length-1; i++){
    newNums[i] = nums[i+1];
    }
    return newNums;
    }

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

    if(nums.length > 0) {
    int temp=nums[0];

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

    nums[nums.length-1] = temp;
    }

    return nums;
    }

    ReplyDelete
  10. With Java Stream and Collections

    public int[] shiftLeft(int[] nums) {
    List numbers = Arrays.stream(nums).boxed().collect(Collectors.toList());
    Collections.rotate(numbers, -1);
    return numbers.stream().mapToInt(i->i).toArray();
    }

    ReplyDelete
  11. public int[] shiftLeft(int[] nums) {
    if(nums.length==0)
    return nums;

    int[] t=new int[nums.length];
    t[nums.length-1]=nums[0];

    for(int i=0;i<nums.length-1;i++){
    t[i]=nums[i+1];

    }
    return t;
    }

    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