Skip to main content
Saving 2 bytes and adding TIO link.
Source Link
Robert Benson
  • 1.6k
  • 11
  • 13

AWK, 25 29 27 bytes

{for(i=0;!($1%++i););$0=i}1

##AWK, 25 29Try it online!

Can save 3 bytes {i=0;while(!($1%++i)){}$0=i}1 by leaving out the i=0 but then multi-line input would be incorrect.

As with all AWK scripts, the code can be placed in a file or typed in at the command line.

Command Line Usage:

awk '{i=0;whilefor(i=0;!($1%++i);){}$0=i;$0=i}1' <<< inputNumber

or place numbers in a FILE each on its own line and do:

awk '{i=0;whilefor(i=0;!($1%++i);){}$0=i;$0=i}1' FILE

CorrectedTwo bytes saved by converting to account for subsequent usages. No longer using uninitialized variablefor loop, also added TIO link.

##AWK, 25 29 bytes {i=0;while(!($1%++i)){}$0=i}1

As with all AWK scripts, the code can be placed in a file or typed in at the command line.

Command Line Usage:

awk '{i=0;while(!($1%++i)){}$0=i}1' <<< inputNumber

or place numbers in a FILE each on its own line and do:

awk '{i=0;while(!($1%++i)){}$0=i}1' FILE

Corrected to account for subsequent usages. No longer using uninitialized variable.

AWK, 25 29 27 bytes

{for(i=0;!($1%++i););$0=i}1

Try it online!

Can save 3 bytes by leaving out the i=0 but then multi-line input would be incorrect.

As with all AWK scripts, the code can be placed in a file or typed in at the command line.

Command Line Usage:

awk '{for(i=0;!($1%++i););$0=i}1' <<< inputNumber

or place numbers in a FILE each on its own line and do:

awk '{for(i=0;!($1%++i););$0=i}1' FILE

Two bytes saved by converting to for loop, also added TIO link.

Fixing bug occuring with subsequent calls
Source Link
Robert Benson
  • 1.6k
  • 11
  • 13

##AWK, 2525 29 bytes {whilei=0;while(!($1%++i)){}$0=i}1

As with all AWK scripts, the code can be placed in a file or typed in at the command line.

Command Line Usage:

awk '{whilei=0;while(!($1%++i)){}$0=i}1' <<< inputNumber

or place numbers in a FILE each on its own line and do:

awk '{whilei=0;while(!($1%++i)){}$0=i}1' FILE

This of course relies on AWK's initializing all variablesCorrected to 0/"" as neededaccount for subsequent usages. No longer using uninitialized variable.

##AWK, 25 bytes {while(!($1%++i)){}$0=i}1

As with all AWK scripts, the code can be placed in a file or typed in at the command line.

Command Line Usage:

awk '{while(!($1%++i)){}$0=i}1' <<< inputNumber

or place numbers in a FILE each on its own line and do:

awk '{while(!($1%++i)){}$0=i}1' FILE

This of course relies on AWK's initializing all variables to 0/"" as needed.

##AWK, 25 29 bytes {i=0;while(!($1%++i)){}$0=i}1

As with all AWK scripts, the code can be placed in a file or typed in at the command line.

Command Line Usage:

awk '{i=0;while(!($1%++i)){}$0=i}1' <<< inputNumber

or place numbers in a FILE each on its own line and do:

awk '{i=0;while(!($1%++i)){}$0=i}1' FILE

Corrected to account for subsequent usages. No longer using uninitialized variable.

Source Link
Robert Benson
  • 1.6k
  • 11
  • 13

##AWK, 25 bytes {while(!($1%++i)){}$0=i}1

As with all AWK scripts, the code can be placed in a file or typed in at the command line.

Command Line Usage:

awk '{while(!($1%++i)){}$0=i}1' <<< inputNumber

or place numbers in a FILE each on its own line and do:

awk '{while(!($1%++i)){}$0=i}1' FILE

This of course relies on AWK's initializing all variables to 0/"" as needed.