Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7539059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:17:02+00:00 2026-05-30T07:17:02+00:00

What is a fast and simple implementation of interleave : console.log( interleave([1,2,3,4,5,6] ,2) );

  • 0

What is a fast and simple implementation of interleave:

console.log( interleave([1,2,3,4,5,6]      ,2) ); // [1,4,2,5,3,6]
console.log( interleave([1,2,3,4,5,6,7,8]  ,2) ); // [1,5,2,6,3,7,4,8]
console.log( interleave([1,2,3,4,5,6]      ,3) ); // [1,3,5,2,4,6]
console.log( interleave([1,2,3,4,5,6,7,8,9],3) ); // [1,4,7,2,5,8,3,6,9]

This mimics taking the array and splitting it into n equal parts, and then shifting items off the front of each partial array in sequence. (n=2 simulates a perfect halving and single shuffle of a deck of cards.)

I don’t much care exactly what happens when the number of items in the array is not evenly divisible by n. Reasonable answers might either interleave the leftovers, or even “punt” and throw them all onto the end.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T07:17:03+00:00Added an answer on May 30, 2026 at 7:17 am

    Since I’ve been pushed to add my own answer early (edited to fix bugs noted by RobG):

    function interleave(items,parts){
      var stride = Math.ceil( items.length / parts ) || 1;
      var result = [], len=items.length;
      for (var i=0;i<stride;++i){
        for (var j=i;j<len;j+=stride){
          result.push(items[j]);
        }
      }
      return result;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How would you solve this problem? (At the beginning it seemed simple, then I
Is it generally fast enough to make simple updates synchronously? For instance with a
Does anyone know of any tools to provide simple, fast queries of flat files
George Marsaglia has written an excellent random number generator that is extremely fast, simple,
I am reading a blogpost at: http://flyingfrogblog.blogspot.com/2009/07/ocaml-vs-f-burrows-wheeler.html a simple implementation for Burrow Wheeler compressing
My simple SQLite FTS3 implementation currently matches user queries with stored results and returns
Looking for a fast, simple way to roll-out a application layer for an existing
I will be quick and simple on this. Basically I need to merge multiple
Just a fast and simple question: how do I add multiple classes to the
How fast is php_uname() say doing php_uname('s n') or php_uname('a') . The reason I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.