1

I have an array that will have just a single non zero value along with the other 0 values at a time. For example, it may be

[0,0,0,0,0,0,0,1234,0,0,0] 
// or
[0,0,2823,0,0,0,0,0,0,0,0]
//...

My question is, how may I get this non zero value from the array using javascript.

I know that I can iterate over the array and return the non zero value as soon as it is found but I was wondering if there is any functionality provided by javascript to do this?

Kamran Ahmed
  • 11,809
  • 23
  • 69
  • 101
  • 3
    @VisioN gave a good answer, but it should be noted that *something* is going to iterate through the array. The functional approach in that answer is nice in that it "covers up" the iteration, but algorithmically it's still a real cost. – Pointy Dec 23 '13 at 13:54
  • @Pointy depends. Can the functional approach be vectorised better than the iterative approach? (I think it's the other way around, and iterative will be faster). – John Dvorak Dec 23 '13 at 13:55
  • @JanDvorak well perhaps, but if you're doing traditional complexity analysis looking up a value in an unordered linear list is an *O(n)* operation. – Pointy Dec 23 '13 at 13:57
  • @Pointy correct, but sixteen-per-clock-cycle type of O(N) is still much better than one-per-sixteen-cycles type of O(N)) :-) – John Dvorak Dec 23 '13 at 13:58
  • Unlike, for example, a C# SortedList, arrays have always to be iterated via brute-force for comparison. Hence, the speediest way is to go from one end through to the other. – Bora Dec 23 '13 at 13:59

2 Answers2

7

You may filter it out from the array:

[0,0,0,0,0,0,0,1234,0,0,0].filter(function(x) { return x; }).pop();  // 1234

As @Sarath mentioned in the comments, if your initial array may have other falsy non numeric values (as false, undefined, '', etc) you may add a strict comparison:

[0,0,0,0,0,0,0,1234,0,0,0].filter(function(x) { return x !== 0; }).pop();

Another short solution for numeric arrays is using reduce method:

[0,0,0,0,0,0,0,1234,0,0,0].reduce(function(a, b) { return a + b; }); // 1234

N.B.: Check the browser compatibility for filter and reduce methods and use polyfills if required.

Community
  • 1
  • 1
VisioN
  • 143,310
  • 32
  • 282
  • 281
  • 1
    @JanDvorak Thanks :) I have just copied the **N.B.** section from [here](http://stackoverflow.com/a/20743730/1249581). – VisioN Dec 23 '13 at 13:57
  • 1
    better user `return x !== 0` what if he wants `'',false,etc` – Sarath Dec 23 '13 at 13:58
  • 1
    @Sarath I assumed a numeric array – John Dvorak Dec 23 '13 at 13:59
  • 1
    @Sarath Yeah, that depends on the input data. The provided method will trim out all falsy values. But if the OP assumes having other than numeric values in the array, your hint will be useful. Thanks. – VisioN Dec 23 '13 at 14:01
3

Just thought I'd offer an alternate solution given the constraints of your question:

var foo = [0,0,0,0,0,0,0,1234,0,0,0],
    bar = [0,0,2823,0,0,0,0,0,0,0,0];

console.log(
    Math.max.apply(null, foo), // 1234
    Math.max.apply(null, bar)  // 2823
);
Emissary
  • 9,954
  • 8
  • 54
  • 65
  • Not a good example, since the value in the array may be negative. – VisioN Dec 23 '13 at 14:33
  • 1
    @VisioN Didn't see the need to expand on the example when that wasn't stipulated in the question. I think it sufficiently illustrates that there are different ways of thinking - which was all I was getting at ;) – Emissary Dec 23 '13 at 14:40