I have the following dataset:
[234,565,678,90,66,7,8,44,5] [275,23,54,34,5] [4745,23,54,4,556,65,89,5,4,569,87,412] [75,273,59,5,4,567,412] [44,34,556,69,5,4,569,812]
Each array represents a web session. Each element in the array represents an activity that the user did during the session.
What's shown above is only a subset of the entire array list (which consists of over million arrays).
My task is to provide the next element, across all arrays, given an element or list of elements.
Here's an example: if
 was given, I need to return
565 because  matches with the first element of the first array. So I need to return the subsequent
But I also need to be able to return a result given an array of elements, like following: if
[275,23,54] was given, I need to return
[275,23,54] matches with the first three elements of the second array, and
34 follows that.
This can be done fairly easily in Java-like language if I cache the entire data set in memory, and then search across those. But my challenge is using a database to store and search these. I was leaning towards key/value databases like Redis. My challenge here is that I don't have a "good" key here. My input could be any of the elements in any array, or a list of elements.
What would be the optimal way of storing and searching this.