MATL, 14 bytes
YP99Y$uj!y=sY"
Try it online!
Explanation with an example
The symbol ;
is used as row separator in matrices. So [1 2 3]
is a row vector, [1; 2; 3]
is a column vector, and [1 2; 3 4]
is a square matrix. The latter can also be represented, for clarity, as
[1 2;
3 4]
Consider input 2325
as an example.
YP % Push approximation of pi as a double (predefined literal)
% 3.14159265358979
99Y$ % Variable-precision arithmetic with 99 digits. Gives a string.
% The input 3.14159265358979 is recognized as representing pi
% STACK: '3.141592653589793238462 ··· 707'
u % Unique entries, keeping order of their first appearance
% STACK: '3.145926870'
j % Input line as a string
% STACK: '3.145926870', '2352'
! % Transpose
% STACK: '3.145926870', ['2'; '3';'5'; '2']
y % Duplicate the second-top element in the stack
% STACK: '3.145926870', ['2'; '3';'5'; '2'], '3.145926870'
= % Test for equality, with broadcast. This gives a matrix with
% all pairwise comparisons)
% STACK: '3.145926870', [0 0 0 0 0 0 1 0 0 0 0;
% 1 0 0 0 0 0 0 0 0 0 0;
% 0 0 0 0 1 0 0 0 0 0 0;
% 0 0 0 0 0 0 1 0 0 0 0]
s % Sum of each column
% STACK: '3.145926870', [1 0 0 0 1 0 2 0 0 0 0]
Y" % Run-length decoding. Implicitly display
% STACK: '3522'