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?

asked Dec 23, 2013 at 13:52
5
  • 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. Commented Dec 23, 2013 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). Commented Dec 23, 2013 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. Commented Dec 23, 2013 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)) :-) Commented Dec 23, 2013 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. Commented Dec 23, 2013 at 13:59

2 Answers 2

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.

answered Dec 23, 2013 at 13:53

4 Comments

@JanDvorak Thanks :) I have just copied the N.B. section from here.
better user return x !== 0 what if he wants '',false,etc
@Sarath I assumed a numeric array
@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.
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
);
answered Dec 23, 2013 at 14:33

2 Comments

Not a good example, since the value in the array may be negative.
@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 ;)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.