Create a list that shows values that did and did not occur
As recommended by @J.M.'stechnicaldifficulties
AssociationThread[datakey, Lookup[Counts[data], datakey, 0]]
produces the result
<|1 -> 2, 2 -> 2, 3 -> 0, 4 -> 3, 5 -> 0, 6 -> 0|>
Thank you for your help.
I think you will find the following much faster for large cases.
With[{t = Tally[Join[datakey, data]]}, Transpose[Transpose[t] - {0, 1}]]
For example:
data = RandomChoice[RandomInteger[{1, 1000000}, 1000000], 10000000];
datakey = Range@1000000;
Length@Union@data
r1 = AssociationThread[datakey,
Lookup[Counts[data], datakey, 0]]; // AbsoluteTiming
r2 = With[{t = Tally[Join[datakey, data]]},
Transpose[Transpose[t] - {0, 1}]]; // AbsoluteTiming
(r1 // Values) == r2[[All, 2]]
2.14736
0.176966
True