| Bytes | Lang | Time | Link |
|---|---|---|---|
| 051 | AWK | 241106T193343Z | xrs |
| 058 | Rust | 220919T092121Z | JSorngar |
| 006 | Husk | 240906T190417Z | int 21h |
| 011 | Z80 machine language | 231205T051411Z | NoLonger |
| 100 | *><> | 231128T221445Z | Bee H. |
| 053 | Trilangle | 230227T195306Z | Bbrk24 |
| 022 | Julia 0.6 | 230914T170759Z | Ashlin H |
| 255 | Nibbles | 230912T112827Z | Dominic |
| 127 | GNU sed | 230912T102154Z | Philippo |
| 002 | Nekomata | 230912T034654Z | alephalp |
| 002 | Vyxal gH | 230403T132606Z | emirps |
| 148 | Pascal | 230304T143216Z | Kai Burg |
| 037 | JavaScript Node.js | 230227T141031Z | Fhuvi |
| 033 | JavaScript ES6 | 230227T173407Z | Arnauld |
| 006 | Stax | 230227T152200Z | emirps |
| 049 | Java 8 OpenJDK 8 | 230227T132213Z | Fhuvi |
| 004 | Brachylog | 220919T083024Z | Fatalize |
| 017 | Arturo | 230104T161917Z | chunes |
| nan | 230226T145721Z | The Thon | |
| 013 | x86_32 machine code | 230105T001553Z | engineer |
| 007 | Pyt | 230107T191428Z | Kip the |
| 008 | Haskell + hgl | 230104T033528Z | Wheat Wi |
| 004 | Vyxal r | 220919T072850Z | tybocopp |
| 026 | Haskell | 220924T084440Z | xnor |
| 004 | Jelly | 220924T045016Z | Unrelate |
| 007 | Pip | 220924T005742Z | DLosc |
| nan | Fig | 220923T211410Z | Seggan |
| 006 | Pyth | 180507T174618Z | hakr14 |
| 005 | Japt | 220919T083320Z | Shaggy |
| 005 | 05AB1E | 220919T083151Z | Kevin Cr |
| 031 | Zsh | 220919T080406Z | pxeger |
| 035 | Prolog SWI | 220904T155606Z | Aiden Ch |
| 052 | Desmos | 220904T154043Z | Aiden Ch |
| 027 | Factor | 220904T092148Z | chunes |
| 007 | Jelly | 170501T134951Z | hyper-ne |
| 016 | Perl 6 | 181010T005919Z | Jo King |
| 004 | MathGolf | 181010T062311Z | Jo King |
| 007 | k | 181011T030116Z | zgrep |
| 028 | Powershell | 181010T075307Z | mazzy |
| 036 | Python 2 | 181010T065056Z | Chas Bro |
| 005 | Stax | 181009T235714Z | recursiv |
| 029 | Wolfram Language Mathematica | 181009T224319Z | nixpower |
| 007 | Jelly | 180507T162031Z | user2027 |
| 011 | APL Dyalog Classic | 180508T053815Z | TwiN |
| 092 | Lua | 180507T203423Z | ivzem |
| 028 | Perl 5 p | 180507T182159Z | Xcali |
| 038 | F# | 180507T180511Z | Ciaran_M |
| 006 | Stax | 180507T174450Z | Multi |
| 013 | J | 140930T041246Z | algorith |
| 007 | Japt | 180507T171631Z | Etheryte |
| 024 | TIBASIC | 170502T000401Z | Scott Mi |
| 019 | APL Dyalog | 170501T173204Z | user4180 |
| 066 | Java 7 | 170501T133645Z | Kevin Cr |
| 091 | Java | 170501T155925Z | Leaky Nu |
| 027 | R | 170501T135738Z | Giuseppe |
| 038 | PHP | 170430T211057Z | Jör |
| 018 | Scala | 170430T204744Z | 2xsaiko |
| 035 | JavaScript | 140930T232123Z | edc65 |
| 023 | Bash+coreutils | 140929T222254Z | Digital |
| 009 | Burlesque 9 Bytes | 140929T225459Z | AndoDaan |
| 064 | C# | 140930T141503Z | DLeh |
| 074 | Javascript | 140930T160250Z | Sean Lat |
| 219 | Scheme | 140930T154711Z | Cruncher |
| 030 | Haskell | 140930T051106Z | proud ha |
| 006 | Pyth | 140930T032322Z | Dennis |
| 050 | Cobra | 140930T034324Z | Οurous |
| 093 | Java | 140930T013627Z | Ypnypn |
| 022 | Ruby | 140929T220628Z | britisht |
| 055 | Xojo | 140929T224700Z | silverpi |
| 037 | Python | 140929T220253Z | Greg Hew |
| 034 | Perl | 140929T215356Z | hmatt1 |
| 007 | Golfscript | 140929T215008Z | paradigm |
| 008 | CJam | 140929T212429Z | Optimize |
| 009 | GolfScript | 140929T212807Z | Peter Ta |
| 032 | Ruby | 140929T212516Z | Martin E |
AWK, -vRS="\\[|,|\\]" 51 bytes
{b[$1]++}END{for(;k++<NR;)if(X~b[k]){print k;exit}}
{b[$1]++} # array indexed on input
END{for(;k++<NR;) # check all possible
if(X~b[k]) # match empty string
{print k;exit}} # escape
Rust, 117, 113, 79, 78 58 bytes
|l:&[u8]|(0..22).filter(|n|!l.contains(n)).next().unwrap()
Husk, 6 bytes
▼-⁰ŀ21
This is my first solution in Husk. I have picked up an easy challenge for starters.
Commented:
ŀ21 # push list 0...20
-⁰ # get the elements not present in the input list
▼ # find the minimum
# output the result
Z80 machine language, 11 bytes
Base address of list in HL, length in BC.
AF C5 E5 ED B1 E1 C1 C0 3C 18 F6
Returns with mex in A unless it's at least 256, then never returns.
If you replace the last two bytes with B7 20 F5 C8 then it returns with mex in A if it's at most 255 and with the zero flag OFF, else with 0 in A and zero flag ON.
xor a
l: push bc
push hl
cpir // 8086: repnz scas
pop hl
pop bc
ret nz
inc a
jr l
The alternative above replaces jr l with:
or a
jr nz,l
ret
*><>, 100 bytes
v v /&~1+I]:&1[D>20.
>l:?!\:1[D&0>:}}:{=?/&1-:&?!\{$}^
;n /! {/
This one's a bit convoluted, so I don't plan on doing a full write-up. The gist, though, is to keep the initial input length (IIL) on a stack above our main one, keep the current index (CI) of the input that we're checking in the register, and to keep a counter value on the stack, leaving it at the 0th index when it comes time to print it.
Checks for a value in the input by starting at the end of the input, and moving forward.
Initialization - The part of Line 1 & 2 I don't mention later
v
>l:?!\:1[D&0
- Ensure the IP is on Line 2, and moving to the right
- Get the length of the input
- If it's
0, move down to Line 3 and print that - Otherwise, duplicate that IIL, and put one copy on a stack above our current one
- And put another copy into the register
Line 1 - Counter value is in input
/&~1+I]:&1[D>20.
- Dump the register
- Collapse the IIL down onto the current stack
- Push a copy of the IIL into the register
- Recreate the stack containing the IIL
- Move the IP to
[0, 2]in the code, placing it before the secondv
Line 2 - Test counter == input[CI], and handle not equal case
>:}}:{=?/&1-:&?!\{$}^
- Ensure IP is moving to the right
- Duplicate the counter and input[CI]
- Move the stack such that a copy of the counter and a copy of the current index are next to each other
- If equal, move to Line 1
- Otherwise, reduce the CI by one
- Check if CI == 0, if so, move to Line 3
- Otherwise,
- Reduce CI by 1
- Move the last element of the input to input[1]
- Move up to Line 1, just before the jump to
[0, 2], essentially creating ado ... while (...)loop
Line 3 - Print
;n /! {/
- If we're printing from Line 2, shift the counter value to the end of the stack
Trilangle, 53 bytes
'0.<_.\.>?0e#L_'\r/(&\,<.|.1.2S<.^#':2',.<..^\,!@.^)S
I'm not convinced this is optimal, but I've thought about it for a while and I don't think I can shrink it without substantial restructuring. I have proven that it's impossible to do this general approach without at least a grid of size n=8, and this is n=10.
Try it on the online interpreter!
Unfolds to this:
'
0 .
< _ .
\ . > ?
0 e # L _
' \ r / ( &
\ , < . | . 1
. 2 S < . ^ # '
: 2 ' , . < . . ^
\ , ! @ . ^ ) S . .
Equivalent to this C code:
#include <stdio.h>
// Read an integer from stdin, throwing out any separators. Returns -1 on EOF.
// Exact implementation is unimportant (and provided by the interpreter).
extern int getint(void);
int main() {
// RED PATH
// Keep a bitfield of the numbers that have been seen so far
int field = 0;
int i;
while ((i = getint()) >= 0)
// GREEN PATH
field |= 1 << i;
// BLUE PATH
// Find the lowest nonzero bit
int missing = 0;
while (field & 1) {
// MAGENTA PATH
field >>= 1;
++missing;
}
// YELLOW PATH
printf("%d\n", missing);
}
I am not doing an instruction-by-instruction walkthrough of this code. If you're interested, you can look into the disassembly feature of the interpreter.
Julia 0.6, 22 bytes
>(x,i=0)=i∈x?x>i+1:i
Minimally adapted from MarcMush's recursive solution to a related challenge.
Julia 0.6 supports ternary operators without spaces, which were deprecated in 0.7 and removed in 1.0.
Nibbles, 2.5 bytes (5 nibbles)
/-`,~
/-`,~ # full program
/-`,~$$ # with implicit args
- $ # remove the input from
`,~ # infinite list 0..infinity
/ $ # and fold from right over the resulting (infinite) list,
# returning the left-hand element each time
GNU sed, 127 bytes
s/.*/ & %-,-0123456789#/;:1;s/%0*(.*)(.)(,.*\2)(.)/%0\1\4\3\4/;s/(.)#(.*\1)(.)/\30\2\3/;/ (\S*) .*%0*\1,/b1;s/.*%0*(..*),.*/\1/
Without the array limit, six more bytes are needed for the inner loop that handles the 99->100 case:
s/.*/ & %-,-0123456789#/
:1
s/%0*(.*)(.)(,.*\2)(.)/%0\1\4\3\4/
:2
s/(.)#(.*\1)(.)/\30\2\3/
t2
/ (\S*) .*%0*\1,/b1
s/.*%0*(..*),.*/\1/
Vyxal gH, 2 bytes
ʀ⊍
Try it Online! (Includes test cases)
# H flag presets the stack to 100
ʀ # inclusive range 0..100
⊍ # set difference
# g flag takes the minimum of the top of stack after execution
# which is implicitly output
Pascal, 148 bytes
This is a complete program meeting the specifications of ISO standard 7185 “Standard Pascal”.
It expects a newline-separated possibly empty list of integers in the range 0..20.
program p(input,output);var i:0..20;s:set of 0..20;begin s:=[];while not EOF do begin readLn(i);s:=s+[i]end;i:=0;while i in s do i:=i+1;write(i)end.
Ungolfed:
program minimumExcludedNumber(input, output);
var
i: 0..20;
s: set of 0..20;
begin
{ transform ordered list to “unordered” `set` ―――――――――――――――――――――― }
{ Initialize `s` as an empty set. }
s := [];
{ This needs to be a head-controlled loop in order to
take account of a possibly _empty_ list of integers. }
while not EOF do
begin
readLn(i);
s := s + [i]
end;
{ analyze step: find first `integer` not part of set ――――――――――――――― }
{ Starting at zero “skip” all _included_ numbers in `s`. }
i := 0;
while i in s do
begin
{ This is guaranteed not to cause an error [this concerns the case
that `i` equals 20], because as per specification “the length of
the list […] will […] be between 0 and 20 inclusive.” Specifying
all integers in the interval `0..20` would require _21_ items. }
i := i + 1
end;
write(i)
end.
See also natural supremum code golfing task and answer.
JavaScript (Node.js), 37 bytes
Only 4 bytes above the best JS solution :'(
l=>l.sort().map(b=>a=a<b?a:b+1,a=0)|a
JavaScript (ES6), 33 bytes
Thanks to @fhuvi for letting me know that my answer to this more recent challenge also works here.
a=>a.reduce(m=>m+a.includes(m),0)
Stax, 6 bytes
21r|^h
This is my first answer in Stax, so I may be missing some builtin that helps.
Explanation
21r|^h #
21r # range 0..20
|^ # setwise difference with the input
h # first element
Java 8 (OpenJDK 8), 49 bytes
Solution using only inline stream :
l->l.stream().sorted().reduce(0,(a,b)->a<b?a:b+1)
Brachylog, 4 bytes
ℕ≜¬∈
Works for any list size and any integer magnitudes.
Takes input from the Output variable, and outputs to the Input variable.
Explanation
ℕ We want a non-negative integer
≜ Assign a value such that…
¬∈ …it is not an element of the list
Arturo, 27 17 bytes
$=>[min--@0..20&]
$=>[ ; a function
min ; minimum of
-- ; difference of
@0..20 ; 0..20, reified
& ; and input
] ; end function
Thunno, \$ 9 \log_{256}(96) \approx \$ 7.41 bytes
21RAq0sAh
Explanation
21RAq0sAh # Implicit input INPUT: [4, 1, 0, 2]
21R # Push range(21) STACK: [4, 1, 0, 2], [0..20]
Aq # Check whether each of the items STACK: [1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
# in the input occurs in range(21)
0sAh # Find the index of the first 0 STACK: 3
# (0-indexed)
# Implicit output
x86_32 machine code, 14 13 bytes
-1 byte thanks to l4m2
Expects the array in EDI (as a uint8_t*) and the length in ECX as an integer. Returns output in EAX.
31 c0 9e 60 f2 ae 61 75 03 40 eb f7 c3
Explanation
; eax=int calc_mex(edi=uint8_t* nums, ecx=int length)
calc_mex:
31 c0 xor eax, eax ; N to look for (start at 0)
; Store 0 to EFLAGS so that when length is zero and scasb never runs,
; ZF is set to zero to return the proper result (0).
9e sahf
loop:
60 pushad ; Store ECX and EDI
; Search for AL in *EDI. If it doesn't exist, return AL; if it does,
; increment AL and search again.
f2 ae repne scasb
61 popad ; Restore ECX and EDI
; If AL was not found (ZF=0), return as we found the minimum.
75 03 jnz return
; Otherwise, keep looping.
40 inc eax
eb f7 jmp loop
return:
c3 ret
Pyt, 7 bytes
05²Ř←\↓
05²Ř generate [0,1,2,...,25]
← get input
\ set difference
↓ get minimum; implicit print
Haskell + hgl, 8 bytes
he<bD nn
Explanation
bD nnall non-negative integers not in the inputheget the first.
Reflection
This is a very straight-forward solution to a very simple problem. The only thing I might say is that it could possibly be worth having a built-in for "The smallest natural number such that ...". Assuming that would be 3 bytes the built in would save 2 bytes in this case.
Vyxal r, 7 5 4 bytes
₇ʀFg
-2 bytes thanks to lyxal
-1 byte thanks to Steffan
Try it online or verify all test cases.
Explanation:
ʀ # The range 0 to
₇ # 128
F # Filter out items from the range that are in the input list
# (The r flag swaps the arguments)
g # Get the lowest one
Jelly, 5 4 bytes
0ḟ1#
Doesn't use the bounds on the input.
...am I missing something?
0 Starting with 0,
1# find the first integer
ḟ which is not in the input.
Pip, 7 bytes
WiNgUii
Takes input as separate command-line arguments. Try It Online!
Explanation
WiNgUii
W While
i a number (initially 0)
N is an element of
g the list of command-line args:
Ui Increment the number
i Output the final value
Fig, \$5\log_{256}(96)\approx\$ 4.116 bytes
[FxmN
Works for all numbers.
[FxmN
mN # All natural numbers [0, 1, 2, 3, ...]
Fx # Remove the inputs
[ # Take the first one remaining
Pyth, 6 bytes
f!}TQZ
Makes no assumptions on the contents of the list.
Explanation:
f!}TQZ | Full code
-------+--------------------------------
f Z | First T starting at 0 such that
!}TQ | T is not in the input
Japt, 5 bytes
@øX}f
@øX}f :Implicit input of array U
@ :Function taking an integer X as an argument
øX : Does U contain X?
} :End function
f :First integer >=0 that returns false when passed through that function
Japt -g, 5 bytes
Lo kU
Lo kU :Implicit input of array U
L :100
o :Range [0,L)
kU :Remove the elements of U
:Implicit output of first element
05AB1E, 6 5 bytes
₂ÝIKн
-1 byte thanks to @emanresuA
Try it online or verify all test cases.
Explanation:
₂Ý # Push a list in the range [0,26]
# (26 is the smallest 1-byte constant above 19)
IK # Remove all values of the input-list from this ranged list
н # Pop and push the first integer remaining (which is the minimum)
# (which is output implicitly as result)
Desmos, 52 bytes
L=[0...20]
f(l)=L[[(i-join(e,l))^2.minfori=L]>0].min
Factor, 27 bytes
[ 21 iota swap diff first ]
! { 3 1 0 1 3 3 }
21 ! { 3 1 0 1 3 3 } 21
iota ! { 3 1 0 1 3 3 } { 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 }
swap ! { 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 } { 3 1 0 1 3 3 }
diff ! { 2 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 }
first ! 2
Jelly, 7 bytes
Ṁ‘‘Ḷḟ⁸Ḣ
How it works
Ṁ‘‘Ḷḟ⁸Ḣ Main Link; argument is z
Ṁ‘‘ Takes 2 above the largest element in z
Ḷ Takes lowered range (thus returning [0, ..., max(z) + 1])
ḟ⁸ Exclude all elements from the range that are also in z
Ḣ Take the first element (which is the smallest)
Thanks to @LeakyNun for -1 byte!
Perl 6, 18 16 bytes
-2 bytes thanks to nwellnhof
{(0...*∉@_)-1}
Anonymous code block that counts up until it finds a number that is not an element of the input list, and returns the length of the range minus one
MathGolf, 5 4 bytes
Jr,╓
This solution is restricted to just the range 0 to 20, though this can be extended easily by increasing the initial range.
Explanation:
Jr Range from 0 to 20
, Remove elements from the input list from this range
╓ Return the minimum element
Alternatively, a 5 byte solution for all numbers:
Åï╧▲ï
Explanation:
Å ▲ Do while true
╧ Does the input contain
ï The index of the loop?
ï Push the number of iterations of the last loop
k, 7 bytes
*(!22)^
(!22) /[0, 1, 2, ..., 21]
^ /set difference between [0, 1, 2, ..., 21] and input (called exclude)
* /get first element
For an arbitrary positive maximum instead of 20, the smallest I have so far is 13 bytes:
{*(!2+|/x)^x}
Powershell, 28 bytes
for(;+$i-in$args){$i++}+$i
Test script:
$f = {
for(;+$i-in$args){$i++}+$i
#for(;$i++-in$args){}(--$i) # alternative version
}
@(
,(0 , 1)
,(1 , 0)
,(2 , 3, 1, 0, 1, 3, 3)
,(0 )
,(0 , 1, 2, 3)
,(3 , 5, 4, 1, 5, 4, 8, 2, 1, 5, 4, 0, 7, 7)
,(4 , 3, 2, 1, 0)
,(4 , 0, 0, 1, 1, 2, 2, 3)
,(10, 1, 0, 7, 6, 3, 11, 15, 1, 9, 2, 3, 1, 5, 2, 3, 4, 6, 8, 1, 18)
) | % {
$e, $a = $_
$r = &$f @a
"$($r-eq$e): $r"
}
Output:
True: 0
True: 1
True: 2
True: 0
True: 0
True: 3
True: 4
True: 4
True: 10
Explanation:
- Increment
$iwhile the$argsarray contains the integer value+$i. - Output a last integer value
+$i.
Stax, 5 bytes
wiix#
w run until condition is satisfied
ii push iteration index twice
x# number of times iteration index appears in input (non-zero is truthy)
When the loop is finished, the final iteration index will be on top of the main stack, and implicitly printed.
Jelly, 7 bytes
Another approach. Can be used in a chain with any arity, and doesn't need chain separator or anything.
‘Ṭ;0i0’
Because the answer is guaranteed to be less than 256, this also works:
Jelly, 5 bytes
⁹ḶḟµḢ
Lua, 92 bytes
x=function(a,...)q=...or 0 for i=0,#a do if a[i]==q then return x(a,q+1)end end return q end
This seems way too long.
F#, 38 bytes
let f s={0..20}|>Seq.except s|>Seq.min
Fairly straight-forward to be honest...
Stax, 6 bytes
¢╔⌂♀╠▬
Explanation
21r:IUI # Full program, unpacked
21 # Push 21
r # Range from 0...20
:I # Find all elements in input that exist in range
U # push -1
I # Get index of first occurrence of
J - 13 char
f=:0{i.@21&-.
Very simple actions in J, and thus very hard to make smaller.
i.@21 creates a list from 0 to 20 inclusive. -. performs set-subtracts the input from this list. 0{ takes the first element of what's left, i.e. the smallest number. f=: defines a named function. At the REPL:
f=:0{(i.21)&-.
f 1
0
f 0
1
f 2 0
1
f 3 1 0 1 3 3
2
f '' NB. empty list
0
f 1 2 3
0
f 5 4 1 5 4 8 2 1 5 4 0 7 7
3
f 3 2 1 0
4
f 0 0 1 1 2 2 3
4
f 1 0 7 6 3 11 15 1 9 2 3 1 5 2 3 4 6 8 1 18
10
Since the release of J806 in November 2017, a new syntax exists which saves us one byte by letting us use i.@21 for the old (i.21) in this context.
Japt, 7 bytes
@!UøXÃa
@ Ãa # Starting from zero, find the smallest integer
!UøX # that's not present in the input array.
TI-BASIC, 24 bytes
:0→A //Store 0 to A
:Prompt X //Prompt list X
:While not(prod(ʟX-A //While A is not missing from list X
:A+1→A //Increment A
:End //End While loop
:A //Print A
If Prompt X is given a list instead of a single number, it will automatically create a list named X that can be accessed with ʟX.
APL (Dyalog), 19 bytes
(0⍳⍨⊢=⍳∘⍴)∘(⊂∘⍋⌷⊢)∪
I am probably missing out something important here. Golfing in progress...
Java 7, 69 66 bytes
int c(java.util.List a){int r=0;for(;a.contains(r);r++);return r;}
-3 bytes thanks to @LeakyNun
Explanation:
Supports not only 0-20, but 0-2147483647 instead (which actually saves bytes).
int c(java.util.List a){ // Method with List parameter and integer return-type
int r=0; // Return integer
for(;a.contains(r);r++); // Continue raising `r` as long as the list contains the current `r`
return r; // Return result-integer
} // End of method
Test code:
import java.util.ArrayList;
import java.util.Arrays;
class M{
static int c(java.util.List a){int r=0;for(;a.contains(r);r++);return r;}
public static void main(String[] a){
System.out.println(c(Arrays.asList(1)));
System.out.println(c(Arrays.asList(0)));
System.out.println(c(Arrays.asList(2, 0)));
System.out.println(c(Arrays.asList(3, 1, 0, 1, 3, 3)));
System.out.println(c(new ArrayList()));
System.out.println(c(Arrays.asList(1, 2, 3)));
System.out.println(c(Arrays.asList(5, 4, 1, 5, 4, 8, 2, 1, 5, 4, 0, 7, 7)));
System.out.println(c(Arrays.asList(3, 2, 1, 0)));
System.out.println(c(Arrays.asList(0, 0, 1, 1, 2, 2, 3)));
System.out.println(c(Arrays.asList(1, 0, 7, 6, 3, 11, 15, 1, 9, 2, 3, 1, 5, 2, 3, 4, 6, 8, 1, 18)));
}
}
Output:
0
1
1
2
0
0
3
4
4
10
Java, 91 bytes
int f(int[]a){int i=0,j=1,k;for(;j>0;i++)for(k=j=0;k<a.length;j=a[k++]==i?1:j);return i-1;}
R, 27 bytes
Reads from stdin; computes the first element in the set difference between [0..20] and x.
x=scan()
setdiff(0:20,x)[1]
R, 36 bytes
which.min(y%in%x) returns the index of the first element of y that is not in x.
x=scan()
y=0:20
y[which.min(y%in%x)]
PHP, 38 Bytes
<?=min(array_diff(range(0,20),$_GET));
PHP, 39 Bytes
<?for(;in_array($i++,$_GET););echo$i-1;
Scala, 18 bytes
0 to 20 diff l min
l is a list of Int.
scala> val l = List(0,1,5)
l: List[Int] = List(0, 1, 5)
scala> 0 to 20 diff l min
res0: Int = 2
JavaScript (E6) 35
Recursive function, array parameter in input and returning the mex. Not limited to 20
F=(l,i=0)=>~l.indexOf(i)?F(l,++i):i
Test in FireFox/FireBug console
;[[1],[0],[2, 0],[3, 1, 0, 1, 3, 3],[],[1, 2, 3],
[5, 4, 1, 5, 4, 8, 2, 1, 5, 4, 0, 7, 7],[3, 2, 1, 0],[0, 0, 1, 1, 2, 2, 3],
[1, 0, 7, 6, 3, 11, 15, 1, 9, 2, 3, 1, 5, 2, 3, 4, 6, 8, 1, 18]]
.forEach(list => console.log(list, F(list)))
Output
[1] 0
[0] 1
[2, 0] 1
[3, 1, 0, 1, 3, 3] 2
[] 0
[1, 2, 3] 0
[5, 4, 1, 5, 4, 8, 2, 1, 5, 4, 0, 7, 7] 3
[3, 2, 1, 0] 4
[0, 0, 1, 1, 2, 2, 3] 4
[1, 0, 7, 6, 3, 11, 15, 1, 9, 2, 3, 1, 5, 2, 3, 4, 6, 8, 1, 18] 10
Bash+coreutils, 23 bytes
seq 0 20|egrep -vwm1 $1
This assumes input as a | (pipe) separated list. E.g:
$ ./mex.sh "5|4|1|5|4|8|2|1|5|4|0|7|7"
3
$
Burlesque - 9 Bytes
20rzj\\<]
Takes input from stdin in the format {7 6 5 5 1 2 2 4 2 0}
Explained:
20 rz map a range from 0 to 20. (thanks to algorithmshark for the cocde fix)
j \\ swaps the two arrays, and collects the difference between the two into a new array
<] gets the smallest element of the resulting array.
Try some examples:
C# - 64 chars
int f(int[] a){return Enumerable.Range(0,20).Except(a).First();}
Not always Rarely the best golfing language, but is easy to write and understand :)
Javascript, 74
i=-1;a=prompt().split(',');while(i<21&&a.indexOf(String(++i))>=0);alert(i)
Nice and simple! Note the empty while loop.
Scheme - 219
(define (A X) (define (B X) (if (equal? (length X) 1) (+ (car X) 1) (if (< (- (cadr X) (car X)) 2) (B (cdr X)) (+ (car X) 1)))) (if (empty? X) `() (if (equal? (car (sort X <)) 0) (B (sort X <)) (- (car (sort X <)) 1))))
Not very competitive. But I like writing scheme :),
Here's the ungolfed code:
(define (minExclude X)
(define (firstNonOneDifference X)
(if (equal? (length X) 1)
(+ (car X) 1)
(if (< (- (cadr X) (car X)) 2)
(firstNonOneDifference (cdr X))
(+ (car X) 1)
))
)
(let ([s (sort X <)])
(if (empty? X)
`()
(if (equal? (car s) 0)
(firstNonOneDifference s)
(- (car s) 1)
))
)
)
Haskell, 30
f s=filter(`notElem`s)[0..]!!0
This works for lists of all size and lists beyond 20. This can be made 15 bytes long if Data.List is imported:
f s=[0..]\\s!!0
Pyth, 6 bytes
h-U21Q
Example run
$ pyth -c h-U21Q <<< '[5, 4, 1, 5, 4, 8, 2, 1, 5, 4, 0, 7, 7]'
3
How it works
U21 range(21)
Q eval(input())
-U21Q setwisedifference(range(21), eval(input)) # Pyth function. Preserves order.
h-U21Q setwisedifference(range(21), eval(input))[0]
Cobra - 50
def f(l)
for n in 22,if n not in l,break
print n
Java, 93
int f(int[]a){int i=0,j=0,k=a.length;for(;i++<20&j<k;){for(j=0;j<k&&a[j++]!=i;);}return i-1;}
Ungolfed:
int f(int[] a) {
int i = 0, j = 0, length = a.length;
for (; i < 20 & j < length; i++) {
for (j = 0; j < length && a[j] != i; j++) { }
}
return i - 1;
}
Ruby, 22
x=->n{([*0..20]-n)[0]}
Explanation
- Input is taken as an argument to a lambda. It expects an
ArrayofIntegers. - The input is subtracted from the array
[0,1,2..20]. - Because the
Array[0,1,2..20]is sorted, the first element must be the mex.
Xojo, 55 bytes
dim x as int8
while a.indexOf(x)>-1
x=x+1
wend
return x
Python, 37 characters
f=lambda a:min(set(range(21))-set(a))
Perl - 34
Here's a subroutine.
sub f{$_~~@_?1:return$_ for0..20}
Test with:
perl -e'print f(0,1,3,4,5,6,7); sub f{$_~~@_?1:return$_ for 0..20}'
Golfscript 7
~21,^0=
A further-golfed version of Peter Taylor's answer. Community wiki since I don't have the rep to comment on his post.
The difference is using the known max list size from the question instead of length +1 to save a character and dropping the irrelevant $.
CJam, 11 8 bytes
K),l~^1<
How it works:
K), "Create an array with numbers 0 through 20"
l~ "Read the input and eval it, resulting to an array"
^ "XOR the elements of two arrays, resulting in a complement array"
1< "Take the first element of the resultant array"
Sample input:
[1 0 7 6 3 11 15 1 9 2 3 1 5 2 3 4 6 8 1 18]
Output:
10
GolfScript (10 9 bytes)
~.,),^$0=
Takes input from stdin in the format [5 4 1 5 4 8 2 1 5 4 0 7 7].
Ruby, 32 bytes
f=->n{(0..20).find{|i|n-[i]==n}}
Defines a function f to be called with an array.
