1

I have a sparse array; that is, an array in which some elements have been deleted. Elements are constantly being added and deleted from the middle, so it is very large and very sparse.

At some point, every element will be undefined, but the length won't be 0 — so it is empty, but it doesn't express that. How can I check if this has happened?

The best that I can think of is to loop through the whole monster and check if they're all undefined. But that would be too inefficient for my application.

11
  • 1
    If possible, you could keep a separate counter that keep tracks of the number of elements in the array. Commented Apr 7, 2014 at 19:30
  • There is a way to check each element's value without actually looking at each element. But with some more bookkeeping, you could check if the # of elements in the array is 0. Commented Apr 7, 2014 at 19:30
  • 1
    You may want to consider using an object (with numeric keys) instead of an array. The only drawback is that you cannot iterate the items in key order. Commented Apr 7, 2014 at 19:34
  • 1
    You could also use a for...in loop to iterate over the array/array. The run time grow linearly with the number of elements in the array. If you delete the elements, there won't be any so the loop won't be executed. Like this var count = 0; for (var _ in obj) { count++: } Commented Apr 7, 2014 at 19:40
  • 1
    See stackoverflow.com/questions/4994201/is-object-empty Commented Apr 7, 2014 at 19:42

5 Answers 5

2

Two solutions I can think of. Both require some modification to your interactions with the array.

First solution

Keep a counter.

If you simply keep a counter that represents how many elements in the array are actually there, then the emptiness check is a mere comparison.

var n = 0;

// Adding something to the array:
myArr[2] = somethingDefined;
n ++;

// Removing from the array:
delete myArr[2];
n --;

// Check if empty
if(n == 0) {
   console.log("myArr is empty.");
}

Second solution

Use splice instead of delete

splice will actually modify the array instead of setting the values to undefined. Example:

myArr.splice(2, 1); // Get rid of myArr[2]

And this mutates the array and will change .length so you can easily check the actual length of the array. Note that splice is less efficient (and given you seem to care about the efficiency, the first solution is probably more applicable).

Sign up to request clarification or add additional context in comments.

2 Comments

If you're gonna keep a counter, I would strongly suggest encapsulating this functionality to enforce the counter to be in sync. It's too easy to forget it and that can be a real pain in the *** bug.
Keeping a counter is not technically a solution, but OK. Better than the alternative.
2

You can use the array method some that quits when it hits any non falsy value:

a.some(Boolean); // returns false if the array has no truthy values

or, if false is a possible value, check for not === undefined:

a.some(function(itm){
   return itm !==undefined;
});

Comments

1

Though this may not be the most efficient, it does the trick quite succinctly:

Object.keys(myArr).length === 0

It might be appropriate for less performance-intensive applications.

5 Comments

That's certainly easier-to-read (and probably write) code, but it is even less efficient than simply iterating over the array.
@Chris: Yeah, I was afraid of that. I put it there so it'd be useful to one-liner-hunters in the future.
@Chris: I just ran this test on Chrome and nearly had a heart attack. Object.keys is faster! Sadly, this doesn't hold true on Firefox.
Huh, very interesting! I'm surprised. Thanks for sharing.
@Chris: Well, your brute force method is much faster and makes sense. If you only want to know whether the array/object is empty or not, then you can exit the loop once you found the first none empty element.
0

You can simulate the removal of your list by using a call to splice:

myArr.splice(indexToRemoveAt,nubmerOfElementsToRemove);

So for your use case:

myArr.splice(indexToRemoveAt,1)

Comments

0

Worked for me sparseArray.size()==0

Comments

Your Answer

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.