Suppose we have a collection of mutually exclusive sets
{A, B, C, D}, where A = {1,2,3}, B = {4,5,6}, C = {7,8,9}, D = {10,11,12}
And given the value of Z , for example, 3, I expect it to return the index of the set A, because A has 3 as a member.
The question is how could I use C ++ or JAVA effectively.
My current solution: Store A, B, C, D as a HashSet (or unordered_set in C ++) in the container and skip each set until the set containing Z.
The problem is that the complexity is O (n) for the number of sets stored in the container.
Is there a way (or any data structure for storing these sets) to do this faster than O (n)?
source share