Reputation: 130431
given 2 arrays (with max length of lets say 15):
var a = [1,2,3];
var b = [5,6,7,8,9,10,11,12];
var data = [13,14,15,16,17,18,19,20,21,22,23,24,25,26];
what would be the best approach to "fill" arrays a
and b
equally with the data from data
array, so they both be the same length (or as close to it) but not exceed max length?
required result could look like this:
var a = [1,2,3,13,14,15,16,17,18,19,20,21];
var b = [5,6,7,8,9,10,11,12,22,23,24,25,26];
Upvotes: 0
Views: 255
Reputation: 1286
var b = [1,2,3];
var a = [5,6,7,8,9,10,11,12];
var data = [13,14,15,16,17,18,19,20,21,22,23,24,25,26];
// find how many more elements need to be added to the smaller array
var diff = Math.abs(b.length - a.length);
// get the elements to add to the smallest array
var data1 = data.splice(0, diff + ((data.length - diff) / 2));
// data now only contains the elements that will not be added to the smallest array
if (a.length < b.length) {
// a is the smallest array
a.push.apply(a, data1);
b.push.apply(b, data);
} else {
// b is the smallest array
b.push.apply(b, data1);
a.push.apply(a, data);
}
Upvotes: 0
Reputation: 130431
my solution was this:
var a = [1,2,3];
var b = [5,6,7,8,9,10,11,12];
var data = [13,14,15,16,17,18,19,20,21,22,23,24,25,26];
var howManyToFill = (data.length + (b.length + a.length)/2)/2;
// take half of the "data" and put it in "a"
a = a.concat(data.slice(0,howManyToFill));
// what's left goes to "b"
b = b.concat(data.slice(howManyToFill));
console.log(a);
console.log(b);
Upvotes: 0
Reputation: 2857
var a = [1,2,3];
var b = [5,6,7,8,9,10,11,12];
var data = [13,14,15,16,17,18,19,20,21,22,23,24,25,26];
for( var i = data.length; i--; ){
if (a.length > b.length)
b.push(data[i])
else
a.push(data[i])
}
console.log(a.length, b.length);
Upvotes: 1
Reputation: 13809
Taken from Brad's comment, I imagine you could do something like this:
ab=[];
ab=ab.concat(a,b,data);
del = (a.length + b.length + data.length) / 2;
a = [];
b = [];
for (var i = 0; i < Math.ceil(del); i++) {
a.push(ab[i]);
b.push(ab[Math.ceil(del) + i]);
}
Upvotes: 1
Reputation: 107347
you want to the last index of a and b have 12 deference and len of final array is 12!!! i think u can use this algorithm ! :
for j in data:
while ( j-max(a)) == 12 ):
x++
for i in range(x):
data[i]=a[i]
and after this delete the indexes with index number mor than 12 !
Upvotes: 0
Reputation: 25565
Here is another way without having to loop through the elements:
var a = [1,2,3],
b = [5,6,7,8,9,10,11,12],
data = [13,14,15,16,17,18,19,20,21,22,23,24,25,26];
var targetLength = (a.length + b.length + data.length)/2,
aCnt = Math.ceil(targetLength) - a.length,
bCnt = Math.floor(targetLength) - b.length;
a = a.concat(data.slice(0, aCnt));
b = b.concat(data.slice(aCnt, data.length));
// a is [1, 2, 3, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]
// b is [5, 6, 7, 8, 9, 10, 11, 12, 23, 24, 25, 26]
Upvotes: 1
Reputation: 563
Algorithm would be like:
1) Identify the number of elements in a
2) Identify the number of elements in b
3) Identify the number of elements in data
4) if elements in a
is less than b
, then populate a from data
till it becomes equal to b
or vice versa
5) if in both a
and b
elements are equal, and data
still having the elements, then populate the array a
and b
alternatively.
Upvotes: 0