C# Missing Integer
static int MissingInteger(int[] a)
{
var min = 1;
var distinct = new HashSet<int>(a).ToList();
distinct.Sort();
foreach (var v in distinct)
{
if (v > 0)
{
if (min != v)
{
break;
}
min++;
}
}
return min;
}
This records the positive numbers that exist, then returns the smallest positive value that is still missing.
Elixir Missing Integer
defmodule MissingInteger do
def missing_integer(a) do
sorted = a |> Enum.uniq() |> Enum.sort()
Enum.reduce_while(sorted, 1, fn v, min ->
cond do
v <= 0 -> {:cont, min}
min != v -> {:halt, min}
true -> {:cont, min + 1}
end
end)
end
end
This records the positive numbers that exist, then returns the smallest positive value that is still missing.
Erlang Missing Integer
-module(missing_integer).
-export([missing_integer/1]).
missing_integer(A) ->
Positives = [V || V <- lists:usort(A), V > 0],
find_missing(Positives, 1).
find_missing([], Min) ->
Min;
find_missing([V | Rest], Min) ->
case V =:= Min of
true -> find_missing(Rest, Min + 1);
false -> Min
end.
This records the positive numbers that exist, then returns the smallest positive value that is still missing.
Go Missing Integer
func missingInteger(a []int) int {
seen := make(map[int]bool, len(a))
for _, v := range a {
seen[v] = true
}
unique := make([]int, 0, len(seen))
for v := range seen {
unique = append(unique, v)
}
sort.Ints(unique)
min := 1
for _, v := range unique {
if v > 0 {
if min != v {
break
}
min++
}
}
return min
}
This records the positive numbers that exist, then returns the smallest positive value that is still missing.
Haskell Missing Integer
import qualified Data.Set as Set
missingInteger :: [Int] -> Int
missingInteger a = go 1 (Set.toAscList (Set.fromList (filter (> 0) a)))
where
go m [] = m
go m (v : vs)
| m /= v = m
| otherwise = go (m + 1) vs
This records the positive numbers that exist, then returns the smallest positive value that is still missing.
Java Missing Integer
import java.util.Arrays;
public class Solution {
public static int missingInteger(int[] a) {
int min = 1;
int[] sorted = Arrays.stream(a).distinct().sorted().toArray();
for (int v : sorted) {
if (v > 0) {
if (min != v) {
break;
}
min++;
}
}
return min;
}
}
This records the positive numbers that exist, then returns the smallest positive value that is still missing.
Lisp Missing Integer
(defun missing-integer (a)
(let ((sorted (sort (remove-duplicates (copy-list a) :test #'=) #'<))
(min 1))
(loop for v in sorted
do (when (> v 0)
(if (/= min v)
(return)
(incf min))))
min))
This records the positive numbers that exist, then returns the smallest positive value that is still missing.
PHP Missing Integer
function missingInteger(array $a): int
{
$min = 1;
$a = array_keys(array_flip($a));
sort($a);
foreach ($a as $v) {
if ($v > 0) {
if ($min !== $v) {
break;
}
$min++;
}
}
return $min;
}
This records the positive numbers that exist, then returns the smallest positive value that is still missing.
Python Missing Integer
def missing_integer(a: list[int]) -> int:
min_val = 1
for v in sorted(set(a)):
if v > 0:
if min_val != v:
break
min_val += 1
return min_val
This records the positive numbers that exist, then returns the smallest positive value that is still missing.
Rust Missing Integer
use std::collections::HashSet;
fn missing_integer(a: &[i64]) -> i64 {
let mut vals: Vec<i64> = a.iter().copied().collect::<HashSet<_>>().into_iter().collect();
vals.sort();
let mut min = 1i64;
for v in vals {
if v > 0 {
if min != v {
break;
}
min += 1;
}
}
min
}
This records the positive numbers that exist, then returns the smallest positive value that is still missing.