Indexize a number
Haskell, 69 bytes
import Data.List
f x=0+read(nub$sortOn(\d->(sum$elemIndices d x,d))x)
Takes a string, returns a number. Usage example: f "30043376111"
-> 47631
. Try it online!
Pretty straight forward: sort the digits of the input string first on the sum of their indices and the by the digit itself (-> pairs of (sum ...,d)), remove duplicates and convert to a number to remove leading 0
. The 0+
is needed to get the types right.
Stacked, 59 bytes
:@q uniq[:q\eq q size:>*sum,]map[-1#]sortby[0#]map''#`'^0'-
Try it online!
This takes a character string (like $'1231231'
) as input from the top of the stack, and leaves a string on the stack.
Explanation
:@q uniq[:q\eq q size:>*sum,]map stack: (str)
: stack: (str str)
@q stack: (str) ; store as `q`
uniq stack: (str') ; de-duplicate
[ ]map map the inner over each element
: stack: (chr chr)
q\eq stack: (chr q') ; `q'` is where equality occurs
q size:> stack: (chr, q', k) ; `k` is range from 0, size(q')
*sum stack: (chr, k') ; `k'` is sum of indices
, stack: ((chr, k'))
Now we are left with pairs of (chr, sum of indices).
[-1#]sortby[0#]map''#`'^0'-
[ ]sortby sort by the inner function
- vectorized subtraction of two pairs
1# use the second element as the comparison
[0#]map get the first element of each row
''#` join by the empty string
'^0'- remove all leading zeroes
05AB1E, 29 28 bytes
-1 thanks to Riley
TFN¹SQDg<ÝsÏON‚}){vyD0å_i1è,
Try it online!
TFN } # Loop from 0 to 9.
¹SQ # Push 1 if index is same as `y`.
Dg<ÝsÏ # Push index of the number instead of 1.
ON‚ # Sum, combine with current `y`.
){ # Collect, sort 'em.
vyD0å_i1è, # Only print the ones with a count above 0.