0

I have an array (1000 pieces) of brands, where I would like to provide a fast search. So when I start typing ie: "m" then I should get "mammut" and "millet". The array is sorted. So does anybody know a fast solution which doesn't need to loop through the whole array? Best in javascript. Thanks

var brands = new Array("arcterix", "mammut", "millet", "ortovox", ... )

function search(brands, substring){
 // will return array of founded brands which begins on substring
}
8
  • well you have to loop through it once at least.... Than each character you can loop over the subset... Commented Dec 3, 2018 at 13:46
  • I think you are a beginner and don't about search engines. You should use ElasicSearch : elastic.co or Solr: lucene.apache.org/solr or if it a basic task than you have loop as suggested by @epascarello Commented Dec 3, 2018 at 13:49
  • @epascarello I am not sure if I need to loop it once at least because when I type for exampe "m" than I know I need to search somewhere in middle and use a binary search. Or am I wrong? Commented Dec 3, 2018 at 13:55
  • @ArwinEdward somehow you need to know where m is.... So yes you can do a search to figure out where the start of "m" is. ou can try it and see if it is faster than just looping. Commented Dec 3, 2018 at 13:55
  • 2
    As noted, you probably don't need anything fancy for your particular use case. That said, if you are interested in implementing a binary search function, see Binary Search in JavaScript Commented Dec 3, 2018 at 15:19

1 Answer 1

2

Try this

var brands = ["arcterix", "mammut", "millet", "ortovox"]

function search(brands, substring){
 return brands.filter( i => i.startsWith(substring) )
}

console.log(search(brands, 'm')) // ['mammut', 'millet']

This is very fast. It's almost impossible to do things faster, everything is optimized for you.

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

5 Comments

This is indeed the best and the only proper way to do this for just 1000 items :) However, it is far from being the fastest algorithm and it is not "optimized". This algorithm works for O(n*pLen) where pLen is the length of a prefix. The main point here is that input array is sorted and therefore it can be improved by utilizing binary search.
@YeldarKurmangaliyev yes, but the question feels to me like the author is quite a newbie. So I've infered that this solution is nothing more than what he needs.
@NurbolAlpysbayev Perfect! :)
@YeldarKurmangaliyev and is there a binary search library for js, or I need to implement myself?
@ArwinEdward You would have to implement it yourself, it is pretty simple algorithm, but its usage case is tricky, because you need find both left-most and right-most occurence, and then do the same recursively for all following letters. But...as already mentioned by Nurbol, it simply won't bring any improvement for 1000 items. No need to overengineer it. Binary search would make sense here, if you get closer to millions of items. I just corrected the statement that it is "very fast and it's almost impossible to do things faster" :)

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.