MATL, 15 bytes
1)VG"@VX&nv@V]x
I've added a simple 'if/else' statement in the TIO link, for those who don't believe that a single 0 makes an array falsey in MATL.
Explanation:
G" ] % Main 'for' loop, to loop over input G
@V % Push current number, convert to string
X& % Intersect with stringified number already on the stack.
n % Count the size of the intersection
v@ % Push the stringified number for the intersection in the next loop.
1)V % For the first iteration, the first element of the input is stringified
% (guaranteed not to have an empty intersection)
Sanchises
- 9.5k
- 1
- 37
- 63