When I input a certain data item (character here), I can access the elements it relates to like:
When I input ‘A’, it gives me access to the values (2, 3, 4, 5), e.g.:
A - 2,3,4,5
B - 6,7,9
C - 10, 11, 12, 13
D - 1,8
and so on...
Also that A, B, C, D could be any data item, int or even a string.
What I am thinking is, I can hold on a linear array and then each item in the array be the header for a linked list. Is this a correct and optimal solution to the above data structure required? Do we already have some data structure to do this?
The best solution is to use a Hash Table with an array (or list) in the table value.
Here an example in Java using HashMap
You could also use
ArrayListinstead of arrays for your integers.The code would become as follows: