This happens to be in JavaScript, but the question applies also to other languages.
I have this very long list of words, sorted alphabetically, such as:
var myList= [
{word:"abstract", flavor:"old", extraData:...},
{word:"aircraft", flavor:"old", extraData:...},
{word:"airplane", flavor:"new", extraData:...},
{word:"banana", flavor:"old", extraData:...},
{word:"calories", flavor:"new", extraData:...},
...
];
My goal is to use some search method (probably a binary search), in order to find how many words start with a given substring. In the example above, given the substring "air" - the result should be 2.
However, sometimes I need to search the whole list, while other times I need to search only the "old" items (which should result in 1 per the example above).
An obvious solution is to duplicate the list, such as:
var wholeList= [
{word:"abstract", flavor:"old", extraData:...},
{word:"aircraft", flavor:"old", extraData:...},
{word:"airplane", flavor:"new", extraData:...},
{word:"banana", flavor:"old", extraData:...},
{word:"calories", flavor:"new", extraData:...},
...
];
var oldList= [
{word:"abstract", flavor:"old", extraData:...},
{word:"aircraft", flavor:"old", extraData:...},
{word:"banana", flavor:"old", extraData:...},
...
];
This is of course very wasteful in terms of memory. Any other/known solutions for such a problem?