Fastest way to search for an element in unsorted array

后端 未结 10 1801
北恋
北恋 2020-12-14 08:13

I just bumped on to this question today and was trying for a solution that is better than O(N) but could not come up with one.

Searched through SO but couldn\'t find

10条回答
  •  甜味超标
    2020-12-14 09:06

    If you're searching for one element once, just iterate through it. No possible way to get it faster.

    If you're searching multiple times, it would be worth it to index it (or sort it, if you will) and make the following searches fast (log(n)).

提交回复
热议问题