There are multiple related questions, but I'm looking for a solution specific to my case. There is an array of (usually) 14 integers, each in the range of 1 to 34. How can I quickly tell if each int in a specific, static list appears at least once in this array?
For reference, I'm currently using this code, which was written to resemble the spec as closely as possible, so it can certainly be improved vastly:
if (array.Count < 13) {
return;
}
var required = new int[] {
0*9 + 1,
0*9 + 9,
1*9 + 1,
1*9 + 9,
2*9 + 1,
2*9 + 9,
3*9 + 1,
3*9 + 2,
3*9 + 3,
3*9 + 4,
3*9 + 5,
3*9 + 6,
3*9 + 7,
};
IsThirteenOrphans = !required.Except (array).Any ();
The required list is not dynamic, i.e. it will always be the same during runtime. Using Linq is optional, the main aspect is performance.
Edit:
- The input array is not sorted.
- Input values may appear multiple times.
- The input array will contain at least 14 items, i.e. 1 more than the required array.
- There is only 1 required array and it is static.
- The values in required are distinct.
- You may assume that a histogram is cheap to create.
Update: I am also interested in a solution for a sorted input array.