Merge Two Arrays So That the Values Alternate

Merge two arrays with alternating values

You could iterate the min length of both array and build alternate elements and at the end push the rest.

var array1 = ["a", "b", "c", "d"],    array2 = [1, 2],    result = [],    i, l = Math.min(array1.length, array2.length);    for (i = 0; i < l; i++) {    result.push(array1[i], array2[i]);}result.push(...array1.slice(l), ...array2.slice(l));
console.log(result);

Merge Two Arrays so that the Values Alternate

You can use the map method:

var array1 = [1, 2, 3, 4, 5];var array2 = ['a', 'b', 'c', 'd', 'e'];
var arrayCombined = $.map(array1, function(v, i) { return [v, array2[i]];});
console.log(arrayCombined);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Alternating values from two arrays into a new third array

To fix your current code, you need to separately check whether i < array1.length (and if so, push array1[i]), and also do the same sort of test for array2:

function mix(array1, array2) {  let newList = [];  for (let i = 0; i < array1.length || i < array2.length; i++) {    if (i < array1.length) {      newList.push(array1[i]);    }    if (i < array2.length) {      newList.push(array2[i]);    }  }  return newList;}console.log(mix([10, 9], ['a', 'b', 'c']));

Merge two arrays in alternate fashion

In your for loop, when you copy from arr2 you need to increment your count.

arr3[count] = arr2[i];
count++;

You could also simplify your code a bit like,

static int[] mergeArray(int[] arr1, int[] arr2) {
int[] arr3 = new int[arr1.length * 2];
int count = 0;
for (int i = 0; i < arr1.length; i++) {
arr3[count++] = arr1[i];
arr3[count++] = arr2[i];
}
return arr3;
}

And print in main like

int arr3[] = mergeArray(arr1, arr2);
System.out.println(Arrays.toString(arr3));

or in Java 8+, use a flatMap and IntStream like

static int[] mergeArray(int[] arr1, int[] arr2) {
return IntStream.range(0, arr1.length)
.flatMap(i -> IntStream.of(arr1[i], arr2[i])).toArray();
}

for the same result.

C# Merging two arrays alternatively

Assuming length of numbers is 1 or longer and always 1 element longer than the length of operations:

string[] numbers = { "1", "2", "3", "4" };
string[] operations = { "+", "+", "+" };

string[] merged = new string[numbers.Length + operations.Length];
merged[0] = numbers[0];

for (int i = 1, j = 1; i < numbers.Length; i++)
{
merged[j++] = operations[i - 1];
merged[j++] = numbers[i];
}

How to merge two arrays, alternating values from each array, in perl

Here is one way to do it. May not be the best perl syntax, though. The double for loop means you will be pushing every combination of the two arrays into the merged array. I've tried to set it up so that the result is in the order you specified in the question.

my @merged;

for my $a ( @a1 ) {
for my $b ( @b1 ) {
push( @merged, ($a, $b) );
}
}

Merge array values, alternating

You can use a for loop and refer to the index of each of the arrays you're combining.

$l = count($vars);
for ($i=0; $i < $l; $i++) {
$combined[] = $vars[$i];
$combined[] = $mods[$i];
}

In each iteration of the loop, you'll append one item from each of the original arrays. This will achieve the alternating effect.


As Steve pointed out, this can be done more simply using foreach:

foreach ($vars as $i => $value) {
$combined[] = $value;
$combined[] = $mods[$i];
}

How do I merge two arrays into a singular array in an alternating order in C?

I would go for something like this

void EvenOdd(int n, int*ap1, int*ap2, int*ap3){
// "n" is set to 5.
ap1=arr1;
ap2=arr2;
ap3=arr3;
int i;
for(int i = 0; i < 2 * n; i++)
{
if(i%2)
{
*ap3= *ap2;
ap1++;
}
else
{
*ap3= *ap1;
ap2++;
}
ap3++;
}
}

Efficiently merge two arrays by distribute values evenly

Find the ratio of the two arrays' lengths, longest.length/shortest.length and then take that many from the longest for every one in the shortest.

let array1 = ["a", "b", "c", "d", "e"];
let array2 = [1, 2];

const evenDistribute = (array1, array2) => {
const longest = array1.length > array2.length ? array1 : array2;
const shortest = array1.length > array2.length ? array2 : array1;
const ratio = Math.floor(longest.length / shortest.length);
const results = [];
for (let i = 0; i < shortest.length; i++) {
for (let j = 0; j < ratio; j++) {
results.push(longest[i * ratio + j]);
}
results.push(shortest[i]);
}
// Grab any that are left over
for (let i = longest.length - (longest.length % shortest.length); i < longest.length; i++) {
results.push(longest[i]);
}
return results;
}

console.log(evenDistribute(array1, array2));


Related Topics



Leave a reply



Submit