Rust Perm Check
fn perm_check(mut a: Vec<i64>) -> i64 {
a.sort();
for (k, &i) in a.iter().enumerate() {
if k + 1 < a.len() && i != k as i64 + 1 {
return 0;
}
}
1
}
This validates that every value from 1 to N appears exactly once.
TypeScript Perm Check
function permCheck(a: number[]): number {
const sorted = [...a].sort((x, y) => x - y);
for (let k = 0; k < sorted.length; k++) {
if (sorted[k + 1] !== undefined && sorted[k] !== k + 1) {
return 0;
}
}
return 1;
}
This validates that every value from 1 to N appears exactly once.
Bash Perm Missing Element
perm_missing_element() {
local -n _a="$1"
local -a _sorted=($(printf '%s\n' "${_a[@]}" | sort -n))
local _k
for _k in "${!_sorted[@]}"; do
if (( _sorted[_k] != _k + 1 )); then
echo $(( _k + 1 ))
return
fi
done
echo $(( ${#_sorted[@]} + 1 ))
}
This uses the expected sum of 1..N+1 and subtracts the actual sum to find the missing value.
C++ Perm Missing Element
#include <algorithm>
#include <cstddef>
#include <vector>
long long permMissingElement(std::vector<int> a)
{
std::sort(a.begin(), a.end());
for (std::size_t k = 0; k < a.size(); ++k) {
if (a[k] != static_cast<int>(k) + 1) {
return static_cast<long long>(k) + 1;
}
}
return static_cast<long long>(a.size()) + 1;
}
This uses the expected sum of 1..N+1 and subtracts the actual sum to find the missing value.
C# Perm Missing Element
static int PermMissingElement(int[] a)
{
var sorted = (int[])a.Clone();
Array.Sort(sorted);
for (int k = 0; k < sorted.Length; k++)
{
if (sorted[k] != k + 1)
{
return k + 1;
}
}
return sorted.Length + 1;
}
This uses the expected sum of 1..N+1 and subtracts the actual sum to find the missing value.
Elixir Perm Missing Element
defmodule PermMissingElement do
def perm_missing_element(a) do
sorted = Enum.sort(a)
sorted
|> Enum.with_index()
|> Enum.find_value(fn {v, k} -> if v != k + 1, do: k + 1 end)
|> Kernel.||(length(sorted) + 1)
end
end
This uses the expected sum of 1..N+1 and subtracts the actual sum to find the missing value.
Erlang Perm Missing Element
-module(perm_missing_element).
-export([perm_missing_element/1]).
perm_missing_element(A) ->
find_missing(lists:sort(A), 1).
find_missing([], Expected) ->
Expected;
find_missing([V | Rest], Expected) ->
case V =:= Expected of
true -> find_missing(Rest, Expected + 1);
false -> Expected
end.
This uses the expected sum of 1..N+1 and subtracts the actual sum to find the missing value.
Go Perm Missing Element
func permMissingElement(a []int) int {
sorted := append([]int(nil), a...)
sort.Ints(sorted)
for k, v := range sorted {
if v != k+1 {
return k + 1
}
}
return len(sorted) + 1
}
This uses the expected sum of 1..N+1 and subtracts the actual sum to find the missing value.
Haskell Perm Missing Element
import Data.List (sort)
permMissingElement :: [Int] -> Int
permMissingElement a = go (zip [1 ..] (sort a))
where
go [] = length a + 1
go ((expected, v) : rest)
| v /= expected = expected
| otherwise = go rest
This uses the expected sum of 1..N+1 and subtracts the actual sum to find the missing value.
Java Perm Missing Element
import java.util.Arrays;
public class Solution {
public static int permMissingElement(int[] a) {
int[] sorted = a.clone();
Arrays.sort(sorted);
for (int k = 0; k < sorted.length; k++) {
if (sorted[k] != k + 1) {
return k + 1;
}
}
return sorted.length + 1;
}
}
This uses the expected sum of 1..N+1 and subtracts the actual sum to find the missing value.