Get a set of binary search objects

I have several such data:

ID  Value  
1   AAA  
1   ABC  
2   dasd  
2   dsfdsf  
2   dsfsd  
3   df  
3   dwqef  

these are objects (not plain text).
and I want to get all objects with ID = 2.
I can do a binary binary search and get index 3, but how can I get (2 and 4), is there an efficient algorithm?
the real problem is lists with about one million items.

any language except bf and lisp can help.

+3
source share
1 answer

You can create a map that maps an identifier to a collection of values;

Map:
1 => { AAA, ABC }
2 => { dasd, dsfdsf, dsfsd }
...

This will have an effective search time of O (1). You can also perform a binary search, but the search will be less efficient.

:

(arraylist, ..). . , , id. , id. .

:

List Index, Object
0    Id=1 Value=A
1    Id=2 Value=B 
2    Id=2 Value=C
3    Id=3 Value=D
4    Id=3 Value=E

2. 1: Id = 2, , 0: Id = 1, , , . 3: Id = 3, . .

+1

Source: https://habr.com/ru/post/1749530/


All Articles