Skip to content

Instantly share code, notes, and snippets.

@forrest-akin
Created October 1, 2020 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save forrest-akin/9e954e69632a0d7ecacc8f14f82303a2 to your computer and use it in GitHub Desktop.
Save forrest-akin/9e954e69632a0d7ecacc8f14f82303a2 to your computer and use it in GitHub Desktop.
const firstMissingPositive =
xs => {
for ( const x of xs )
indexByValue( x , xs )
let ndx = 0
while ( xs[ ndx ] === ++ndx ) {}
return ndx
}
const indexByValue =
( x , xs ) =>
xs[ x - 1 ] !== x
&& 0 < x
&& x <= xs.length
&& ( y = xs[ x - 1 ]
, xs[ x - 1 ] = x
, indexByValue( y , xs )
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment