43
\$\begingroup\$

You will be given a string. It will contain 9 unique integers from 0-9. You must return the missing integer. The string will look like this:

123456789
 > 0

134567890
 > 2

867953120
 > 4
\$\endgroup\$
9
  • 5
    \$\begingroup\$ @riker That seems to be about finding a number missing in a sequence. This seems to be about finding a digit missing from a set. \$\endgroup\$
    – DJMcMayhem
    Commented Mar 27, 2017 at 19:48
  • 12
    \$\begingroup\$ @Riker I wouldn't think it's a duplicate, given that the linked challenge has a strictly incrementing sequence (of potentially multi-digit numbers), whereas here it's in arbitrary order. \$\endgroup\$ Commented Mar 27, 2017 at 19:50
  • 3
    \$\begingroup\$ Hi Josh! Since no one else has mentioned it so far, I'll direct you to the Sandbox where you can post future challenge ideas and get meaningful feedback before posting to main. That would have helped iron out any details (like STDIN/STDOUT) and resolved the duplicate dilemma before you received downvotes here. \$\endgroup\$ Commented Mar 27, 2017 at 20:23
  • 1
    \$\begingroup\$ It's such a shame that 9-x%9 works for any digit except 0. Maybe someone more clever than me will find a way to make it work. \$\endgroup\$
    – Bijan
    Commented Mar 28, 2017 at 0:45
  • 3
    \$\begingroup\$ Several answers take an integer as function input. Is that allowed? \$\endgroup\$
    – Dennis
    Commented Mar 28, 2017 at 0:52

92 Answers 92

3
\$\begingroup\$

Befunge 98, 14 12 bytes

I saved 1 byte by moving the program onto 1 line and 1 byte by doing some better math

~+;@.%a--7;#

Try it online!

Explanation

The sum of the ASCII values range from 477 to 468 depending on which number is missing. By subtracting this from 7, we get the range -470 to -461. By modding this number by 10, we get the range 0 - 9, which we can then print.

~+;       ;#    Sums the ASCII values of all characters to stdIn
~          #    The # doesn't skip over the ~ because it's on the end of a line
~               Once EOF is hit, the ~ reverses the IP's direction
          ;#    Jump the ; that was used before
       --7      Subtract the sum from 7 (really just 0 - (sum - 7))
     %a         Mod it by 10
   @.           Print and exit

The reason I use the ASCII values instead of taking integer input is because the & command in Try it Online halts on EOF (Even though it should reverse the IP). The ~ works correctly, though.

Old Program, 14 bytes

#v~+
@>'i5*--,

The sum of the ASCII values of all 10 digits is 525. By subtracting the sum of the given digits from 525, we get the ASCII value of the missing character.

#v~+         Sums the ASCII values of all characters on stdIn
             Moves to the next line when this is done
 >'i5*       Pushes 525 (105 * 5)
      --     Subtracts the sum from 525
@       ,    Prints and exits
\$\endgroup\$
3
\$\begingroup\$

K (ngn/k), 5 bytes

477-/

Try it online!

Subtracts the ASCII character codes of the input string from 477.

\$\endgroup\$
3
\$\begingroup\$

Vyxal, 3 bytes

kd⊍

Try it Online!

Imagine not having set operations.

Explained

kd⊍
kd    # "0123456789"
  ⊍   # set(↑) ^ set(input)
\$\endgroup\$
1
3
\$\begingroup\$

Risky, 5 bytes

+0/+/-_?}*

Input and output are integers. Try it online!

Explanation

Here's a rough diagram of the program's parse tree:

    +
    -
  0   _
 +     }
/ /   ? *

It calculates the set difference between the list of numbers 0 through 9 and the list of digits in the input (example input 134567890):

  /         5
  /+/       5+5 = 10
 0/+/       range(10) = [0,1,2,3,4,5,6,7,8,9]
       ?    input number = 134567890
         *  10
       ?}*  to-base(134567890, 10) = [1,3,4,5,6,7,8,9,0]
      _?}*  no-op
 0/+/-_?}*  diff([0,1,2,3,4,5,6,7,8,9], [1,3,4,5,6,7,8,9,0]) = [2]
+0/+/-_?}*  sum([2]) = 2

If input as a list of digits is acceptable, here's a 3-byte solution:

+0*-_?

It's the same idea, just skips the base-conversion step.

\$\endgroup\$
3
\$\begingroup\$

Fig, \$4\log_{256}(96)\approx\$ 3.292 bytes

Fxcd

Try it online!

Fxcd
Fx   # Remove all elements of the input
  cd # From the string "0123456789"
\$\endgroup\$
2
\$\begingroup\$

Röda, 28 bytes

{(_/"")|ord _|sum|chr 525-_}

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Pyth, 5 Bytes

-jkUT

try it!

explanation

-jkUT
    T   # 10
   U    # The unary range of ten: [0,1,..,9]
 jk     # join that on the empty string
-       # set minus

"-jUT" also kinda works but produces newlines for every int.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ The interpreter at the provided link throws errors, this one works: Try it online! \$\endgroup\$
    – mik
    Commented Mar 15, 2021 at 16:44
2
\$\begingroup\$

C (tcc), 36 31 bytes

f(long*s){s=9-(*s+s[1]%16)%15;}

Takes a string as input and returns an int. As written, this work only on little-endian architectures.

The lack of a return statement is undefined behavior, but this works with tcc and gcc.

Try it online!

Alternate version, 33 bytes, no UB

f(long*s){*s=57-(*s+s[1]%16)%15;}

Takes a string pointer as input and overwrites the string with the result (allowed by default).

While this is perfectly valid C, it will not work with compilers such as gcc, which store strings in read-only memory sections.

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ I like the use of longs to compress the information. \$\endgroup\$
    – Bijan
    Commented Mar 28, 2017 at 0:55
2
\$\begingroup\$

J, 9 bytes

Num_j_&-.

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Wren, 31 bytes

Fn.new{|x|"0123456789".trim(x)}

Try it online!

Explanation

Fn.new{|x|                      // New anonymous function with parameter x
          "0123456789"          // Declare all the numbers
                      .trim(x)  // Trim out everything included in x
                              } // The remaining number is the result
\$\endgroup\$
2
\$\begingroup\$

Clojure, 28 bytes

#(- 477(apply +(map int %)))
\$\endgroup\$
2
\$\begingroup\$

Perl 5 -p, 12 bytes

$_=-hex()%15

Try it online!

Explanation:

Interprets input as hex string, negates it, and calculates modulo 15 (based on xnor's Python answer).

\$\endgroup\$
2
\$\begingroup\$

Javascript, 18 17 bytes

-1B from tsh

s=>9-('0x9'+s)%15

F=
s=>9-('0x9'+s)%15
;
console.log(F('135792048'))

\$\endgroup\$
1
  • \$\begingroup\$ use 9-('0x9'+s)%15 save 1 bytes \$\endgroup\$
    – tsh
    Commented Mar 16, 2021 at 3:25
2
\$\begingroup\$

Dash (POSIX shell with POSIX utility ), 50 47 27 bytes

$((1${1%0*}-1$1?9-9$1%9:0))

Try it online!

The code above evaluates to a string of digit.

Alternative

$((1${1#*0}-1$1?9-9$1%9:0))

How it works

  • ${1%0*}. If $1 has 0, then remove digits from right up to 0; else unchanged.
  • ${1#*0}. Similar but from left.
  • Why 1$1 and 9$9? Numbers begining from 0 is octal; but it would be syntax error if $1 has 8 and 9. They are for workarounding.
\$\endgroup\$
0
2
\$\begingroup\$

Excel, 28 bytes

=SUM(45,-MID(A2,ROW(1:9),1))
\$\endgroup\$
2
\$\begingroup\$

Julia 1.0, 22 bytes

~x=(w='0':'9')[w.∉x]

Try it online!

\$\endgroup\$
4
  • 1
    \$\begingroup\$ you can remove the parens around a[.!occursin.(a,x)] and [ and ;] around '0':'9' to save 5 bytes. \$\endgroup\$
    – naffetS
    Commented Oct 8, 2022 at 19:59
  • 1
    \$\begingroup\$ You can also save another two by defining the variable inline: ~x=(w='0':'9')[.!occursin.(w,x)][1] \$\endgroup\$
    – naffetS
    Commented Oct 8, 2022 at 19:59
  • 1
    \$\begingroup\$ 22 bytes using ∉ and printing a single element array \$\endgroup\$
    – amelies
    Commented Oct 9, 2022 at 15:33
  • \$\begingroup\$ Thanks, amelies. I've used your solution and changed this answer to a community wiki. \$\endgroup\$ Commented Oct 9, 2022 at 18:39
1
\$\begingroup\$

QBIC, 25 bytes

;{~instr(A,!a$)|a=a+1\_Xa

Explanation

;       Get the input as A$
{       Start an infinite DO loop
~instr  Test if A$ has an occurrence of a% cast to string       
(A,!a$) a starts out as 0. !..$ casts to string.
|a=a+1  If we did find an instance, tets for the next a
\_Xa    Else, quit, printing our missing number.
\$\endgroup\$
1
\$\begingroup\$

Perl 5, 21 bytes

20 bytes of code + -p flag.

s/./+$&/g;$_=45-eval

Try it online!
Note that the input needs to be supplied without final newline (with echo -n for instance).


Some other (longer) approaches (all with -p flag):

$\=45;$\-=$_ for/./g}{

$_=9876543210=~s/[$_]//gr

for$@(0..9){$\=$@if!/$@/}}{
\$\endgroup\$
1
\$\begingroup\$

C, 44 bytes

j;f(char*s){for(j=477;*s;)j-=*s++;return j;}

Try it online!

\$\endgroup\$
1
\$\begingroup\$

PHP, 53 bytes

There allready was a array_sum and regex solution, wanted to provide another:

print_r(array_diff(range(0,9),str_split($argvs[1])));

A few bytes more, but as bonus it will provide all missing numbers.

\$\endgroup\$
1
\$\begingroup\$

Batch, 83 76 52 bytes

@set/pn=
@cmd/cset/a(641670-0x%n:~,4%-0x%n:~4%)%%15

Takes input on STDIN. Uses @xnor's hex modulo 15 trick, except that a) Batch only has 32-bit integers, so I have to split the string into two b) Batch only does remainder, not modulo, so I have to subtrat the values from a large multiple of 15 first.

\$\endgroup\$
1
\$\begingroup\$

PHP, 43 Bytes

for(;strpos(_.$argv[1],48+$i++););echo$i-1;

PHP, 50 Bytes

<?=join(preg_grep("#[{$argv[1]}]#",range(0,9),1));
\$\endgroup\$
4
  • 1
    \$\begingroup\$ save 1 byte: for(;strpos(_.$argv[1],48+$i++););echo$i-1; \$\endgroup\$
    – Christoph
    Commented Mar 28, 2017 at 6:34
  • \$\begingroup\$ @Christoph I have the feeling that you forget the chr function \$\endgroup\$ Commented Mar 28, 2017 at 9:51
  • 1
    \$\begingroup\$ I have the feeling you should learn your tools ;) If needle is not a string, it is converted to an integer and applied as the ordinal value of a character see strpos on php.net. \$\endgroup\$
    – Christoph
    Commented Mar 28, 2017 at 10:01
  • \$\begingroup\$ @Christoph you are right \$\endgroup\$ Commented Mar 28, 2017 at 10:39
1
\$\begingroup\$

JavaScript, 26 bytes

v=>45-eval([...v].join`+`)

I'm not a big fan of eval, but it does the job. The sum of all digits 0-9 is 45. 45 minus the sum of the passed-in digits is the value of the missing digit.

Test

f=v=>45-eval([...v].join`+`)


function test() {

  var i=I.value;
  O.textContent = f(i)
}  

test()
<input oninput='test()' value='012987654' id=I>
<pre id=O></pre>

\$\endgroup\$
1
\$\begingroup\$

Scala, 9 bytes

477-_.sum

To use it, assign it to a variable:

val f:(String=>Int)=477-_.sum

_ is syntactix sugar for the arguments of a function, so this expands to x => 477 - x.sum, which will subtract the sum of the ascii codes of the input from 477.

\$\endgroup\$
1
\$\begingroup\$

Pure bash, 28

a=1234567890
echo ${a//[$1]}

Try it online.

\$\endgroup\$
1
\$\begingroup\$

PHP, 50 39 Bytes

function x($y){echo(45-array_sum(str_split($y)));}

Jörg Hülsermann's answer prompted me to try the CLI method, Thanks.

echo 45-array_sum(str_split($argv[1]));

Test it at the command line with:

php -r 'echo 45-array_sum(str_split($argv[1]))."\n";' /'12346789'

Test it (The old version) here if you'd like.

Wheat Wizard's answer put me in the right direction and I got help from This Answer

\$\endgroup\$
1
\$\begingroup\$

J, 14 bytes

((i.10)-."."0)

Try it online!

J is always surprisingly inadequate for golfing :(

(                )  NB. Monadic fork: (f g h) x = (f x) g (h x)
  (i.10)            NB. Array of integers from 0 to 9
            "."0    NB. Digits of string (". = evaluate, "0 = each atom)
         -.         NB. Except
\$\endgroup\$
2
  • 1
    \$\begingroup\$ You can save 2 bytes by shortening (i.10)"_ to (i.10) \$\endgroup\$
    – Tikkanz
    Commented Mar 30, 2017 at 3:16
  • \$\begingroup\$ @TIkkanz thanks! I never remember the left verb of a fork can be a noun :( \$\endgroup\$
    – kaoD
    Commented Mar 31, 2017 at 14:48
1
\$\begingroup\$

TXR Lisp, 28 bytes:

This is 28 bytes. It reads a line of digits and yields a string of the missing ones as the result value;

(diff"0123456789"(get-line))

This uses the awk macro to do read every line of input and print the missing digits. It only adds one byte to the length:

(awk((mf(diff"0123456789"))))

At the system prompt:

$ txr -P '(diff"0123456789"(get-line))'
135249
0678

$ txr -e '(awk((mf(diff"0123456789"))))'
123456789
0
234567890
1
012357698
4
13579
02468
^D
\$\endgroup\$
1
\$\begingroup\$

W, 13 7 bytes

'0'9.St

Explanation

'0'9.   % Define the range of string 0 to 9
     S  % Swap so that instructions are in the right order
        % a[0], "0...9" -> "0...9", a[0]
      t % Trim out everything in 0...9 that appears in a[0]
        % The result is the remaining number

W, 8 bytes

CJ525S-C

Explanation

C         % Convert every character to its code point form
 J        % Sum the list
  525S-   % Minus 525 from the value
       C  % Convert to character form
          % Implicit output
\$\endgroup\$
1
\$\begingroup\$

Keg, 14 8 bytes

?⅀ȍ$-

Try it online! -6 bytes thanks to @A̲̲

Answer History

14 bytes

`0123456789`᠀-

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ You could have got this to 8 bytes; 8 bytes because this uses unicode characters. \$\endgroup\$
    – user85052
    Commented Dec 12, 2019 at 13:31

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.