Assume for a moment 64 possible elements.
Then, if you represent each element as a bit, you can use an integer of 64 bits to represent each set, and then: a & b is set the intersection of a and b . If (and only if) a is a subset of b , then a & b == a .
Of course, you can use bitset if you need more than 64 bits.
For a large range of elements, using a hash table to store (once) the add-in, and then iterate through potential subsets to see if all the elements in it can be.
It is linear in input size (average case).
EDIT: (answer to editable question)
If you have not previously saved some information about the data - this cannot be done by betetr, then O(|X| + n*min{m,|X|}) Where | X | is the size of the set X, n is the number of sets, and m is the average size of the sets. The reason is that this happens in the worst case, you need to read all the elements in all the sets (because the last element that you read for each set decides whether it is a subset or not), and therefore we cannot achieve better without previous knowledge of sets.
Suggested solutions:
Bitset: O(|X|*n)
Hash solution: O(|X| + min{m,|X|}*n) (middle case)
Although a hash solution provides better asymptotic complexity, constants are much better for bit set, and therefore a bit rate solution is likely to be faster for small |X|
source share