Shuffle the Array (Javascript)

Joe Avila - Aug 26 '20 - - Dev Community

Algorithms are something I struggle with. A few of the interviews or screening processes for companies I've done recently have involved algorithms. Being eliminated before even getting to speak to someone is very discouraging. In response, I'm being intentional about solving algorithms recently. This time I'll be tackling this Leetcode problem.


Snoopy shuffling

Given the array nums consisting of 2n elements in the form
[x1,x2,...,xn,y1,y2,...,yn].
Return the array in the form [x1,y1,x2,y2,...,xn,yn].

My job is to shuffle the elements of the array together like a deck of cards. This problem is simple with Javascripts .push() method.

var shuffle = function(nums, n) {
    let arr = [];
    for ( let i=0; i<n; i++ ) {
        arr.push(nums[i], nums[i+n])
    };
    return arr;
};
Enter fullscreen mode Exit fullscreen mode
  • Declare an empty array arr which will be my return value.
  • Begin a for loop that will run until the index is greater than n.
  • In each iteration:
    1. push() the current index nums[i] and nums[i + n] into the return array arr.

After iterating n times return our new array.


The difficulty was increased when I approached it without push(). I was having trouble keeping track of the indexes I need access to, so I wrote it out on a whiteboard. After writing it out I was able to find the pattern. Stepping through every iteration really cleared up what counters I needed.

Whiteboard of my solution

I ended up with four variables i, y, x, b. i pointed to the first index for the return array of my code block. y pointed to the second index for the return array. These two variables were incremented by 2 each iteration. x pointed to the first index for the input array. b pointed to the second index for the input array. These two variables were incremented by 1 each iteration. To catch the pattern I really needed to be thorough. After that I was quickly able to deduce what I needed to do. This was my final solution:

var shuffle = function(nums, n) {
    const arr = [];
    let i = 0
    for ( let x=0; x < n; x++) {
        arr[i] = nums[x];
        arr[i+1] = nums[x+n];
        i+=2;
    };
    return arr;
};
Enter fullscreen mode Exit fullscreen mode
  • Declare an empty array arr which will be my return value.
  • Declare a counter i outside of the loop that I'll increment differently.
  • Begin a for loop that will run until the index x is greater than n.
  • In each iteration:
    1. Set arr[i] to nums[x]
    2. Set arr[i+1] to nums[x+n]
    3. Increment i by 2
    4. Check if x < n
  • Return the array.
. . . . . . . . . . . . . . . . .
Terabox Video Player