Find first missing positive: Difference between revisions

m
syntax highlighting fixup automation
No edit summary
m (syntax highlighting fixup automation)
Line 11:
 
=={{header|11l}}==
<langsyntaxhighlight lang="11l">V nums = [[1, 2, 0], [3, 4, -1, 1], [7, 8, 9, 11, 12]]
 
L(l) nums
Line 17:
I n !C l
print(l‘ -> ’n)
L.break</langsyntaxhighlight>
 
{{out}}
Line 27:
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">DEFINE PTR="CARD"
 
BYTE FUNC Contains(INT ARRAY a INT len,x)
Line 89:
arr(3)=a4 arr(4)=a4
Test(arr,lengths,COUNT)
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Find_first_missing_positive.png Screenshot from Atari 8-bit computer]
Line 102:
=={{header|ALGOL 68}}==
Uses the observation in the J sample that the maximum possible minimum missing positive integer is one more than the length of the list.
<langsyntaxhighlight lang="algol68">BEGIN # find the lowest positive integer not present in various arrays #
# returns the lowest positive integer not present in r #
PROC min missing positive = ( []INT r )INT:
Line 132:
print( ( " ", whole( min missing positive( ( 3, 4, -1, 1 ) ), 0 ) ) );
print( ( " ", whole( min missing positive( ( 7, 8, 9, 11, 12 ) ), 0 ) ) )
END</langsyntaxhighlight>
{{out}}
<pre>
Line 140:
=={{header|APL}}==
{{works with|Dyalog APL}}
<langsyntaxhighlight APLlang="apl">fmp ← ⊃(⍳1+⌈/)~⊢</langsyntaxhighlight>
{{out}}
<pre> fmp¨ (1 2 0) (3 4 ¯1 1) (7 8 9 11 12)
Line 147:
=={{header|AppleScript}}==
===Procedural===
<langsyntaxhighlight lang="applescript">local output, aList, n
set output to {}
repeat with aList in {{1, 2, 0}, {3, 4, -1, 1}, {7, 8, 9, 11, 12}}
Line 156:
set end of output to n
end repeat
return output</langsyntaxhighlight>
 
{{output}}
<syntaxhighlight lang ="applescript">{3, 2, 1}</langsyntaxhighlight>
 
 
Line 166:
Defining the value required in terms of pre-existing generic primitives:
 
<langsyntaxhighlight lang="applescript">--------------- FIRST MISSING NATURAL NUMBER -------------
 
-- firstGap :: [Int] -> Int
Line 287:
set my text item delimiters to dlm
s
end unlines</langsyntaxhighlight>
{{Out}}
<pre>{1, 2, 0} -> 3
Line 294:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">First_Missing_Positive(obj){
Arr := [], i := 0
for k, v in obj
Line 305:
m := m ? m : Max(obj*) + 1
return m>0 ? m : 1
}</langsyntaxhighlight>
Examples:<langsyntaxhighlight AutoHotkeylang="autohotkey">nums := [[1,2,0], [3,4,-1,1], [7,8,9,11,12], [-4,-2,-3], []]
for i, obj in nums{
m := First_Missing_Positive(obj)
Line 312:
}
MsgBox % Trim(output, ", ")
return</langsyntaxhighlight>
{{out}}
<pre>3, 2, 1, 1, 1</pre>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f FIND_FIRST_MISSING_POSITIVE.AWK
BEGIN {
Line 349:
exit(0)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 355:
</pre>
=={{header|BASIC}}==
<langsyntaxhighlight lang="basic">10 DEFINT A-Z: DIM D(100)
20 READ X
30 FOR A=1 TO X
Line 379:
230 DATA 3, 1,2,0
240 DATA 4, 3,4,-1,1
250 DATA 5, 7,8,9,11,12</langsyntaxhighlight>
{{out}}
<pre> 1 2 0 ==> 3
Line 386:
 
=={{header|BCPL}}==
<langsyntaxhighlight lang="bcpl">get "libhdr"
 
let max(v, n) = valof
Line 414:
show(4, table 3,4,-1,1)
show(5, table 7,8,9,11,12)
$)</langsyntaxhighlight>
{{out}}
<pre>1 2 0 ==> 3
Line 421:
 
=={{header|BQN}}==
<langsyntaxhighlight lang="bqn">FMP ← ⊢(⊑(¬∊˜ )/⊢)1+(↕1+⌈´)
 
FMP¨ ⟨⟨1,2,0⟩,⟨3,4,¯1,1⟩,⟨7,8,9,11,12⟩⟩</langsyntaxhighlight>
{{out}}
<pre>⟨ 3 2 1 ⟩</pre>
 
=={{header|CLU}}==
<langsyntaxhighlight lang="clu">contains = proc [T, U: type] (needle: T, haystack: U) returns (bool)
where T has equal: proctype (T,T) returns (bool),
U has elements: itertype (U) yields (T)
Line 458:
stream$putl(po, "==> " || int$unparse(fmp[si](test)))
end
end start_up</langsyntaxhighlight>
{{out}}
<pre>1 2 0 ==> 3
Line 465:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// Find first missing positive. Nigel Galloway: February 15., 2021
let fN g=let g=0::g|>List.filter((<) -1)|>List.sort|>List.distinct
match g|>List.pairwise|>List.tryFind(fun(n,g)->g>n+1) with Some(n,_)->n+1 |_->List.max g+1
[[1;2;0];[3;4;-1;1];[7;8;9;11;12]]|>List.iter(fN>>printf "%d "); printfn ""
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 477:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: formatting fry hash-sets kernel math sequences sets ;
 
: first-missing ( seq -- n )
Line 484:
{ { 1 2 0 } { 3 4 1 1 } { 7 8 9 11 12 } { 1 2 3 4 5 }
{ -6 -5 -2 -1 } { 5 -5 } { -2 } { 1 } { } }
[ dup first-missing "%u ==> %d\n" printf ] each</langsyntaxhighlight>
{{out}}
<pre>
Line 499:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">function is_in( n() as integer, k as uinteger ) as boolean
for i as uinteger = 1 to ubound(n)
if n(i) = k then return true
Line 520:
print fmp(a())
print fmp(b())
print fmp(c())</langsyntaxhighlight>
{{out}}<pre>
3
Line 529:
=={{header|Go}}==
{{trans|Wren}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 564:
fmt.Println(a, "->", firstMissingPositive(a))
}
}</langsyntaxhighlight>
 
{{out}}
Line 583:
=={{header|Haskell}}==
{{trans|Wren}}
<langsyntaxhighlight Haskelllang="haskell">import Data.List (sort)
 
task :: Integral a => [a] -> a
Line 599:
map
task
[[1, 2, 0], [3, 4, -1, 1], [7, 8, 9, 11, 12]]</langsyntaxhighlight>
{{out}}
<pre>[3,2,1]</pre>
Line 605:
 
Or simply as a filter over an infinite list:
<langsyntaxhighlight lang="haskell">---------- FIRST MISSING POSITIVE NATURAL NUMBER ---------
 
firstGap :: [Int] -> Int
Line 620:
[3, 4, -1, 1],
[7, 8, 9, 11, 12]
]</langsyntaxhighlight>
 
and if xs were large, it could be defined as a set:
<langsyntaxhighlight lang="haskell">import Data.Set (fromList, notMember)
 
---------- FIRST MISSING POSITIVE NATURAL NUMBER ---------
Line 630:
firstGap xs = head $ filter (`notMember` seen) [1 ..]
where
seen = fromList xs</langsyntaxhighlight>
{{Out}}
Same output for '''notElem''' and '''notMember''' versions above:
Line 642:
Note that the <nowiki>{{ }}</nowiki> delimiters on definitions was introduced in J version 9.2
 
<langsyntaxhighlight Jlang="j">fmp=: {{ {.y-.~1+i.1+#y }}S:0</langsyntaxhighlight>
 
Example use:
 
<langsyntaxhighlight Jlang="j"> fmp 1 2 0;3 4 _1 1; 7 8 9 11 12
3 2 1</langsyntaxhighlight>
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang="javascript">(() => {
"use strict";
 
Line 722:
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre>[1,2,0] -> 3
Line 734:
In case the target array is very long, it probably makes sense either to sort it,
or to use a hash, for quick lookup. For the sake of illustration, we'll use a hash:
<syntaxhighlight lang="jq">
<lang jq>
# input: an array of integers
def first_missing_positive:
Line 747:
# The task:
examples
| "\(first_missing_positive) is missing from \(.)"</langsyntaxhighlight>
{{out}}
<pre>
Line 757:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">
for array in [[1,2,0], [3,4,-1,1], [7,8,9,11,12], [-5, -2, -6, -1]]
for n in 1:typemax(Int)
Line 763:
end
end
</langsyntaxhighlight>{{out}}
<pre>
[1, 2, 0] => 3
Line 775:
In order to avoid the O(n) search in arrays, we could use an intermediate set built from the sequence. But this is useless with the chosen examples.
 
<langsyntaxhighlight Nimlang="nim">for a in [@[1, 2, 0], @[3, 4, -1, 1], @[7, 8, 9, 11, 12], @[-5, -2, -6, -1]]:
for n in 1..int.high:
if n notin a:
echo a, " → ", n
break</langsyntaxhighlight>
 
{{out}}
Line 788:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl -l
 
use strict;
Line 801:
print "[ @$test ] ==> ",
first { not { map { $_ => 1 } @$test }->{$_} } 1 .. @$test + 1;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 817:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
Line 828:
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #7060A8;">papply</span><span style="color: #0000FF;">({{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">7</span><span style="color: #0000FF;">,</span><span style="color: #000000;">8</span><span style="color: #0000FF;">,</span><span style="color: #000000;">9</span><span style="color: #0000FF;">,</span><span style="color: #000000;">11</span><span style="color: #0000FF;">,</span><span style="color: #000000;">12</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">},{-</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">5</span><span style="color: #0000FF;">},{-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},{}}</span> <span style="color: #0000FF;">,</span><span style="color: #000000;">test</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 843:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">'''First missing natural number'''
 
from itertools import count
Line 869:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>[1, 2, 0] -> 3
Line 879:
{{works with|QBasic}}
{{works with|QuickBasic|4.5}}
<langsyntaxhighlight lang="qbasic">DECLARE FUNCTION isin (n(), k)
DECLARE FUNCTION fmp (n())
 
Line 911:
IF n(i) = k THEN isin = 1
NEXT i
END FUNCTION</langsyntaxhighlight>
{{out}}
<pre>3
Line 919:
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>say $_, " ==> ", (1..Inf).first( -> \n { n ∉ $_ } ) for
[ 1, 2, 0], [3, 4, 1, 1], [7, 8, 9, 11, 12], [1, 2, 3, 4, 5], [-6, -5, -2, -1], [5, -5], [-2], [1], []</langsyntaxhighlight>
{{out}}
<pre>[1 2 0] ==> 3
Line 934:
=={{header|REXX}}==
This REXX version doesn't need to sort the elements of the sets, &nbsp; it uses an associated array.
<langsyntaxhighlight lang="rexx">/*REXX program finds the smallest missing positive integer in a given list of integers. */
parse arg a /*obtain optional arguments from the CL*/
if a='' | a="," then a= '[1,2,0] [3,4,-1,1] [7,8,9,11,12] [1,2,3,4,5]' ,
Line 952:
if @.m=='' then m= 1 /*handle the case of a "null" set. */
say right( word(a, j), 40) ' ───► ' m /*show the set and the missing integer.*/
end /*j*/ /*stick a fork in it, we're all done. */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 969:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">nums = [[1,2,0], [3,4,-1,1], [7,8,9,11,12], [1,2,3,4,5],
[-6,-5,-2,-1], [5,-5], [-2], [1], []]
 
Line 988:
if n = len(ary) s = "" ok
res += "" + ary[n] + s
next return res + "]"</langsyntaxhighlight>
{{out}}
<pre>the smallest missing positive integer for [1,2,0]: 3
Line 1,002:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">nums = [1,2,0], [3,4,-1,1], [7,8,9,11,12]
puts nums.map{|ar|(1..).find{|candidate| !ar.include?(candidate) }}.join(", ")</langsyntaxhighlight>
{{out}}
<pre>3, 2, 1</pre>
 
=={{header|True BASIC}}==
<langsyntaxhighlight lang="qbasic">FUNCTION isin (n(), k)
FOR i = LBOUND(n) TO UBOUND(n)
IF n(i) = k THEN LET isin = 1
Line 1,042:
DATA 3,4,-1,1
DATA 7,8,9,11,12
END</langsyntaxhighlight>
 
=={{header|Vlang}}==
{{trans|go}}
<langsyntaxhighlight lang="vlang">fn first_missing_positive(a []int) int {
mut b := []int{}
for e in a {
Line 1,074:
println("$a -> ${first_missing_positive(a)}")
}
}</langsyntaxhighlight>
{{out}}
<pre>Same as go entry</pre>
Line 1,080:
=={{header|Wren}}==
{{libheader|Wren-sort}}
<langsyntaxhighlight lang="ecmascript">import "/sort" for Sort
 
var firstMissingPositive = Fn.new { |a|
Line 1,099:
[-6, -5, -2, -1], [5, -5], [-2], [1], []
]
for (a in aa) System.print("%(a) -> %(firstMissingPositive.call(a))")</langsyntaxhighlight>
 
{{out}}
Line 1,117:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">proc ShowMissing(Arr, Len);
int Arr, Len, N, N0, I;
[N:= 1;
Line 1,132:
ShowMissing( Nums(I), (Nums(I+1)-Nums(I))/4 );
];
]</langsyntaxhighlight>
 
{{out}}
10,333

edits