Within a sparse-array data-structure how does one retrieve the real position count of such a structure's item?

I have a sparse array of array entries/items as follows …

var indexed = new Array();

indexed["5"] = [35, 2];
indexed["3"] = [20, 2];
indexed["4"] = [25, 3];

I want to find something like the "real" position count of an entry/item within such a sparse array structure by providing an item’s array index( respectively its key).

Expected results:

  • If one does provide 3 (either as string or number value) the returned (index) value should be the number value 0.
  • If one does provide 4 (either as string or number value) the returned (index) value should be the number value 1.
  • If one does provide 5 (either as string or number value) the returned (index) value should be the number value 2.
  • In case of an unsuccessful search the return value should be the number value -1.

Leave a Comment