# Output the missing integer

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

• @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. Mar 27 '17 at 19:48
• @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. Mar 27 '17 at 19:50
• 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. Mar 27 '17 at 20:23
• 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. Mar 28 '17 at 0:45
• Several answers take an integer as function input. Is that allowed? Mar 28 '17 at 0:52

# 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.

• The interpreter at the provided link throws errors, this one works: Try it online!
– mik
Mar 15 at 16:44

# 05AB1E, 5 bytes

žhISK


Try it online!

Explanation

žh     # from the string "0123456789"
K  # remove
IS   # each digit of the input

• I like it because you can say it out loud. '"žhISK", he cried, as he waved his wand over the top-hat and a small white rabbit appeared in a puff of smoke'. Mar 27 '17 at 23:50
• SK can now be м to save a byte. Dec 12 '19 at 13:28

# Japt, 6 bytes

¬x n45


Try it online!

### Explanation:

¬x n45
n45     // 45-
¬          //    Split the input into an array "123" → ["1","2","3"]
x         //    Return the sum of all the items ["1","2","3"] → 6
// 45 - 6 = output


# C (tcc), 36 31 bytes

f(long*s){s=9-(*s+s%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%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!

• I like the use of longs to compress the information. Mar 28 '17 at 0:55

# dc, 12 10 bytes

Cdi?B%-B%p


Try the dc version online!

This uses the fact that the sum of the digits from 0 to 9 is 45, which is 1 more than a multiple of 11.

The program works by viewing the input as a base 12 number, finding its remainder when divided by 11, and subtracting that from 12 (to find the missing digit). The only catch is that if 0 or 1 is the missing digit, this would give an answer of 11 or 12, respectively, so I mod out by 11 one additional time at the end to take care of those cases.

This yields a short bash solution also:

# Bash + Unix utilities, 17 15 bytes

dc -eCdi?B%-B%p


Try the bash version online!

# J, 9 bytes

Num_j_&-.


Try it online!

# 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


# Clojure, 28 bytes

#(- 477(apply +(map int %)))


# 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). # Javascript, 18 17 bytes -1B from tsh s=>9-('0x9'+s)%15  F= s=>9-('0x9'+s)%15 ; console.log(F('135792048')) • use 9-('0x9'+s)%15 save 1 bytes – tsh Mar 16 at 3:25 # Vyxal, 3 bytes kd⊍  Try it Online! Imagine not having set operations. ## Explained kd⊍ kd # "0123456789" ⊍ # set(↑) ^ set(input)  ## 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.


# 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!/$@/}}{  # C, 44 bytes j;f(char*s){for(j=477;*s;)j-=*s++;return j;}  Try it online! # 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)));


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

## Batch, 8376 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.

for(;strpos(_.$argv,48+$i++););echo$i-1;  # PHP, 50 Bytes <?=join(preg_grep("#[{$argv}]#",range(0,9),1));

• save 1 byte: for(;strpos(_.$argv,48+$i++););echo$i-1; Mar 28 '17 at 6:34 • @Christoph I have the feeling that you forget the chr function Mar 28 '17 at 9:51 • 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. Mar 28 '17 at 10:01 • @Christoph you are right Mar 28 '17 at 10:39 # MATL, 6 bytes 4Y2jX-  Try it at MATL Online Explanation 4Y2 % Pre-defined literal for '0123456789' j % Grab input as a string X- % Compute the set difference between the two, yields the characters in % '0123456789' that are missing in the input % Implicitly display the result  # 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> # 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. # Pure bash, 28 a=1234567890 echo${a//[$1]}  # Bash, 36 bytes sort <(fold -1<<<$a;seq 0 9)|uniq -u


Try it online!

Posting to get golfing tips over this.

fold -1<<$a writes one char of input per line seq 0 9 writes 0..9 one per line after this. Those lines are fed to sort and filtered by uniq -u displaying only not duplicated lines. 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));


Test it at the command line with:

135249
0678

$txr -e '(awk((mf(diff"0123456789"))))' 123456789 0 234567890 1 012357698 4 13579 02468 ^D  ## 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...9" -> "0...9", a t % Trim out everything in 0...9 that appears in a % 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  # Keg, 14 8 bytes ?⅀ȍ$-


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

### 14 bytes

0123456789᠀-


Try it online!

• You could have got this to 8 bytes; 8 bytes because this uses unicode characters.
– user85052
Dec 12 '19 at 13:31

## TI BASIC, 23 bytes

45-sum(seq(expr(sub(Ans,I,1)),I,1,9


Turns the input string in Ans into a list containg the digits and then sums the list so that the number can be derived from subtracting the sum from 45. The byte count is affected by the 2 byte tokens expr(, sub(, and Str1. A full program could prompt for Str1 for 5 additional bytes.

# BRASCA, 16 bytes

477SSm8[a+A{]x-n


Try it online!

## Explanation

<implicit input>     - Push STDIN to stack
477SSm               - Push 477 (the sum of all digits' ASCII codes) to the bottom
8[    ]        - Loop 9 times:
a+A          -   Put loop counter in register, add the top two digits, then take it back out.
{         -   Decrement the loop counter
x-      - Remove the loop counter, then subtract the sum of all digits from 477
n     - Output the result as a number


# AWK, 18 bytes

{$0=/0/?9-$0%9:0}1


AWK live editor

Explanation: If 0 exists in the input, return 9 - modulo 9 of the input, otherwise return 0. Implicit print.