2

I have an array something like this

a=[
[1,6,6,8,0], 

[7,3,2,6],


[7,3,2]

] 

Here, I need to find the maximum length of the inside array. And I need to replace other with '0'.

For example the first array has 5 elements, which is the maximum length, so I need to put '0' to the 2nd and 3rd array till reaches the maximum lengh (i.e 5). What should I do here?

Ash
  • 239
  • 2
  • 9
  • 25
  • You should be able to find the length of the longest array by extending the Array type. See http://stackoverflow.com/questions/1669190/javascript-min-max-array-values. – Rob Lyndon Jun 24 '13 at 06:01

3 Answers3

6
var max = Math.max.apply(null, a.map(function(i) {
    return i.length;
}));
var new_arr = a.map(function(i) {
    var pad = max - i.length;
    while (pad--) {
        i.push(0);
    } 
    return i;
});

http://jsfiddle.net/Mgfnf/1/

zerkms
  • 249,484
  • 69
  • 436
  • 539
3

As you said, your task is split into two:

  1. Find the maximum array length
  2. Pad the arrays to meet that length

Both #1 and #2 can be achieved through a simple loop through the outer array. In the first, we keep a variable (let's call it maxLength) which will hold, yes, our max length.

var maxLength = 0;
//walk through the array
for (var i = 0; i < a.length; i += 1) {
    //choose the larger
    maxLength = Math.max(maxLength, a[i].length);
}

Now that we have the size we wish to expand to, we go over the outer loop, and on each sub-array, we push 0s until the lengths match:

//walk through the array
for (var j = 0; j < a.length; j += 1) {
    //the length will increase by 1 on each push
    while (a[j].length < maxLength) {
        a[j].push(0);
    }
}

And that's it.

Zirak
  • 38,920
  • 13
  • 81
  • 92
  • 2
    Downvoter, could you please explain? Did I make a mistake somewhere? – Zirak Jun 24 '13 at 06:17
  • 1
    I see no reason for a downvote. In fact, I think this answer might be the most useful for OP. OP should first make sure he knows how to tackle this kind of problem with simple code before even learning the fancy solutions using `apply`. – Denys Séguret Jun 24 '13 at 06:45
0

try this

var i = 0,
    max = 0;

while(i<a.length){ // find max
    max = a[i].length > max ? a[i].length : max;
    i++;
}

i = 0;
while(i<a.length){

    for(var j=a[i].length;j<max;j++){
        a[i][j] = 0;
    }
    i++;
}
AntouanK
  • 4,880
  • 1
  • 21
  • 26
  • It's not immediately clear to me why this was downvoted -- whether for readability or a flaw in the procedure that makes it incorrect. Either way, I find zerkms's solution more readable and expressive. – Rob Lyndon Jun 24 '13 at 06:12
  • it's a bit primitive, I know. but it's just a simple solution that works properly. – AntouanK Jun 24 '13 at 06:14
  • Your padding method is inefficient – John Dvorak Jun 24 '13 at 06:19
  • Not to mention the confused iteration constructs (why is the outer one a `while`?) – John Dvorak Jun 24 '13 at 06:19
  • @JanDvorak and why is that? – AntouanK Jun 24 '13 at 06:20
  • If the array is already rectangular, you read it entirely anyways. `theta(A.length * A[max].length)` instead of `theta(A.length)` best-case and typical-case. – John Dvorak Jun 24 '13 at 06:22
  • @JanDvorak Ok, I agree, I check elements I could avoid. (Assuming that the 'undefined' gaps are always sequential and not between other defined numbers.) I changed that so it doesn't read them. – AntouanK Jun 24 '13 at 06:29
  • Why are you keeping the conditional, then? Don't you already know they are going to be `undefined`? Also, why use `while` for the outer loop but `for` for the inner loop? – John Dvorak Jun 24 '13 at 06:33
  • @JanDvorak yes, the conditional can be also avoided, I thought it was more readable that way. While and for are the same, it's just a style preference I guess. – AntouanK Jun 24 '13 at 06:37
  • @Antonis at least you should pick a consistent style, if not the preferred one ;-). The `for` loop is there in the language for a reason ;-) – John Dvorak Jun 24 '13 at 06:40