[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 208
  • Last Modified:

Searching for a substring in a Map

I have a list of 3000 items.
Each item has key and value.

I need to find all the occurrence in the list whose value matches a substring.

Example:
My input is "home" and my list is:

Key              Value
001              home gardening
002              stay at home
003              office supplies
004              working from home - guide
005              the homeland office

The output should return the following items:
001, 002, 004 and 005.

I can use a vector and using a loop, perform a string.indexOf validation for each item in the list. Is there a more efficient way of doing this.
0
bennies
Asked:
bennies
  • 2
1 Solution
 
CEHJCommented:
>>Is there a more efficient way of doing this.

No - in a word.
0
 
CEHJCommented:
...unless of course you can mark the items in some way on insertion, and then maintain a separate index.
0
 
jimmackCommented:
I'd go along with CEHJs comments here.

There are always *different* ways of doing things, but that all depends on what you mean by efficiency.  Remember, efficiency is always a trade-off between space and speed.

The way you're doing it at the moment is probably about the best you could do in terms of space, implementing a separate index might make the code faster, but increases the space it takes.

It all depends on which (space or speed) you want to sacrifice ;-)

(Note.  Sometimes it is possible to create a solution that is more efficient in both space and speed.  This usually leads to unreadable/unmaintainable code ;-))
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now