40
\$\begingroup\$

Surprisingly we haven't had a simple "find the highest digit" challenge yet, but I think that's a little too trivial.

Given input of a non-negative integer, return the highest unique (ie not repeated) digit found in the integer. If there are no unique digits, your program can do anything (undefined behaviour), other than the numbers that are non unique in the input of course.

The input can be taken as a single integer, a string, or a list of digits.

Test cases

12         -> 2
0          -> 0
485902     -> 9
495902     -> 5
999999     -> Anything
999099     -> 0
1948710498 -> 7

This is so fewest bytes in each language wins!

\$\endgroup\$
11
  • 2
    \$\begingroup\$ Can we take input as a string instead? \$\endgroup\$
    – user41805
    Commented Jun 28, 2017 at 8:13
  • 3
    \$\begingroup\$ Given the last test case, I think we are forced to take input as a string... (leading zeroes can't be represented in integers) \$\endgroup\$
    – Leo
    Commented Jun 28, 2017 at 8:14
  • \$\begingroup\$ @Leo that was my bad actually, basically mashed the numbers on my keyboard, didn't notice the leading zero. But yes, input can be taken as a string \$\endgroup\$
    – Mayube
    Commented Jun 28, 2017 at 8:17
  • 29
    \$\begingroup\$ @Adám "undefined behaviour" generally means you can do anything, including summoning nameless horrors from the void if that saves bytes. \$\endgroup\$ Commented Jun 28, 2017 at 8:22
  • 24
    \$\begingroup\$ @MartinEnder in fact I'll happily knock off 50% of your bytes if your code successfully summons cthulhu upon there being no unique digits ;) \$\endgroup\$
    – Mayube
    Commented Jun 28, 2017 at 8:31

75 Answers 75

1 2
3
0
\$\begingroup\$

C (gcc), 76 bytes

d[58],i;main(){while((i=getchar())>0)++d[i];for(i=58;--d[--i];);putchar(i);}

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Suggest d[58];main(i) instead of d[58],i;main(). Also, suggest (~(i=getchar())) instead of ((i=getchar())>0) \$\endgroup\$
    – ceilingcat
    Commented Aug 21, 2018 at 1:02
0
\$\begingroup\$

PHP, 79 76 bytes

Try it online!

foreach(array_count_values(str_split($n))as$x=>$i){$h=($i==1&&$x>$h)?$x:$h;}

Use with $n = 495902; it will print 5.

\$\endgroup\$
0
\$\begingroup\$

PowerShell v3+, 58 bytes

$i|% ToCharA*|sort|group|? count -lt 2|select -l 1 -exp N*

Assumes $i contains a string of digits.

Try it online!

\$\endgroup\$
0
\$\begingroup\$

F#, 67 bytes

let f s=Seq.countBy id s|>Seq.filter(snd>>(=)1)|>Seq.maxBy fst|>fst

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Kotlin, 53 Bytes

val f={a:String->a.filter{a.count{b->it==b}<2}.max()}

Declares a lambda, f that takes in a string; same algorithm as the python answer. it is the implicit iterator of a.filter, and b is the explicitly declared iterator of a.count (because we can't use it again).

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Pip, 11 bytes

MX:_Na=1FIa

Takes input as a command-line argument. Try it online!

Explanation

          a  1st cmdline arg
        FI   Filter digits by this function:
   _Na        Count of digit in a
      =1      equals 1 (i.e. keep only digits that appear exactly once)
MX:          Max of resulting list (using : to lower precedence)
             Autoprint (implicit)
\$\endgroup\$
0
\$\begingroup\$

CJam, 15 bytes

r$e`{0=1=},e~W=

Explanation:

r    e# Read token:          "2515"
$    e# Sort:                "1255"
e`   e# Run-length encode:   [[1 '1] [1 '2] [2 '5]]
{    e# Filter where:
 0=  e#   The first element: [1 1 2]
 1=  e#   Equals one:        [1 1 0]
},   e# End filter:          [[1 '1] [1 '2]]
e~   e# Run-length decode:   "12"
W=   e# Last digit:          '2
\$\endgroup\$
0
\$\begingroup\$

Braingolf, 15 bytes

k&gG{!L1-?$_|}X

Try it online!

Takes input as a list of digits

Explanation

k&gG{!L1-?$_|}X  Implicit input from commandline args
k                Sort in descending numerical order
 &g              Combine into single integer
   G             Split into digit runs
    {........}   Foreach loop..
     !L1-?       ..If length of item is greater than 1..
          $_     ....Remove item
            |    ..Endif
              X  Select highest value
                 Implicit output

To help visualize it a little, here's a run through showing the stack with input 122355567679

k&gG{!L1-?$_|}X  [1,2,2,3,5,5,5,6,7,6,7,9]
k                [9,7,7,6,6,5,5,5,3,2,2,1]
 &g              [977665553221]
   G             [9,77,66,555,3,22,1]
    {!L1-?$_|}   [9,3,1]
              X  [9]
\$\endgroup\$
0
\$\begingroup\$

k, 12 bytes

|/@&:1=#:'=:

Example:

k)F:|/@&:1=#:'=:
k)F "1948710498"
"7"
k)F "99999"
"\000"
k)F "mississippi"
"m"

The translation to q as explanation

max where 1 = count each group@

Interpreter available here

\$\endgroup\$
1
  • \$\begingroup\$ I think this can be simplified to |/&1=#:'=: (provided it's run as k4 code). \$\endgroup\$
    – coltim
    Commented Nov 4, 2020 at 21:40
0
\$\begingroup\$

J, 19 bytes

[:>./(".*1=#)/.~@":

Explanation:

": converts the number to a string
@ propagates the result (composes the verbs)
/.~ classifies items of the list into partitions of identical items
". * 1=# - fork of 3 verbs:
   1=# - 1 if the key is unique, else 0
   ". converts the keys back to digits
   * multiplies the key values by 1 or 0 depending on their uniqueness
>./ finds the max digit
[: caps the fork, since we have 2 verbs only for the key operator

Example:

   a=.123456547
     ": a
    123456547  NB. string
    </.~@": a
    ┌─┬─┬─┬──┬──┬─┬─┐
    │1│2│3│44│55│6│7│          NB. keys
    └─┴─┴─┴──┴──┴─┴─┘

(1=#)/.~@": a
1 1 1 0 0 1 1                  NB. mask for unique digits

(".*1=#)/.~@": a
1 2 3 0 0 6 7                  NB. digits multiplied by the mask, rendering
                                   duplicates to 0

([:>./(".*1=#)/.~@":) a        NB. finds the max nonrepeating digit
7

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Perl 5 -pF, 36 bytes

$_=join'',sort@F;s/(.)\1+//g;$_=chop

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Stax, 4 bytes

|!|M

Run and debug it

This program takes an array of digits. It works by calculating the maximum "anti-mode". I'm not sure if anti-mode is a real thing outside of stax, but it refers to the rarest element in list.

\$\endgroup\$
0
\$\begingroup\$

dc, 48 bytes

[1+dd;a+r:az0<A]dsAx[d]sD1[dd;a=D1+dB>M]dsMxr1-p

Try it online!

Takes input as digits entered on the stack. I'm assuming this is a fair stand-in for a list of digits, since the justification for that was that folks were just dropping in boilerplate to turn an integer into a list of digits, and the task would be the same here (just... as a stack). Returns '10' on an entry w/ no unique digits.

Macro A builds an array a by taking each digit, loading the value in its own index (this will be 0 if thus far unassigned), adding itself, and storing this new value. We actually have to add 1 to every digit first so that 0 works correctly, and at the end we subtract 1 from our final result to make up for this. Ignoring that briefly for illustrative purposes, any index (let's call it n) of a will now have one of the following: 0 if n didn't exist as a digit in our original number, n itself if n was unique, or some multiple of n other than n*1 if n existed and was not unique.

In dc, conditionals either run a macro if true or do nothing if false, so you need a macro for them to do anything. [d]sD is a macro, D that simply duplicates a value.

A 1 goes on the stack to act as a counter of sorts, and then macro M runs. M really just takes this counter number, loads that index from a, compares it to itself, and duplicates itself (by running D) if they're equal. So, if they aren't equal (digit didn't exist or wasn't unique), we're simply left with one instance of our counter; if they are equal, we now have another copy of it on the stack. Increment, and keep on going 1-10. When this is done, we swap the top two values on the stack and subtract one as mentioned earlier.

\$\endgroup\$
0
\$\begingroup\$

Jelly, 7 bytes

ċⱮ`Ị×⁸Ṁ

Try it online!

Input as a list of digits

How it works

ċⱮ`Ị×⁸Ṁ - Main link. Takes the list D on the left
  `     - Using D as both arguments:
 Ɱ      -   For each element in D:
ċ       -     Count occurrences in D
   Ị    - Insignificant; Yield 1 if abs(x) ≤ 1 else 0
     ⁸  - Yield D
    ×   - Multiply the digits by the insignificants
      Ṁ - Take the maximum
\$\endgroup\$
0
\$\begingroup\$

Factor, 27 bytes

[ non-repeating ?supremum ]

Attempt This Online!

  • non-repeating Get only the elements of a sequence that don't repeat.
  • ?supremum Get the largest element of a sequence, or return f for the empty sequence.
\$\endgroup\$
1 2
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.