int firstMissingPositive(vector<int>& nums) {
int n = nums.size();
for (int candidate = 1; candidate <= n + 1; candidate++) {
bool present = false;
for (int x : nums) {
if (x == candidate) { present = true; break; }
}
if (!present) return candidate;
}
return n + 1;
}
public int firstMissingPositive(int[] nums) {
int n = nums.length;
for (int candidate = 1; candidate <= n + 1; candidate++) {
boolean present = false;
for (int x : nums) {
if (x == candidate) { present = true; break; }
}
if (!present) return candidate;
}
return n + 1;
}
def first_missing_positive(nums):
n = len(nums)
for candidate in range(1, n + 2):
present = False
for x in nums:
if x == candidate:
present = True
break
if not present: return candidate
return n + 1
function firstMissingPositive(nums: number[]): number {
for (let candidate = 1; candidate <= nums.length + 1; candidate++) {
let present = false;
for (const x of nums) {
if (x === candidate) { present = true; break; }
}
if (!present) return candidate;
}
return nums.length + 1;
}
int firstMissingPositive(vector<int>& nums) {
vector<int> sorted(nums);
sort(sorted.begin(), sorted.end());
int expected = 1;
for (int x : sorted) {
if (x == expected) expected++;
else if (x > expected) break;
}
return expected;
}
public int firstMissingPositive(int[] nums) {
int[] sorted = nums.clone();
Arrays.sort(sorted);
int expected = 1;
for (int x : sorted) {
if (x == expected) expected++;
else if (x > expected) break;
}
return expected;
}
def first_missing_positive(nums):
sorted_nums = sorted(nums)
expected = 1
for x in sorted_nums:
if x == expected: expected += 1
elif x > expected: break
return expected
function firstMissingPositive(nums: number[]): number {
const sorted = [...nums].sort((a, b) => a - b);
let expected = 1;
for (const x of sorted) {
if (x === expected) expected++;
else if (x > expected) break;
}
return expected;
}
int firstMissingPositive(vector<int>& nums) {
unordered_set<int> seen(nums.begin(), nums.end());
int n = nums.size();
for (int candidate = 1; candidate <= n; candidate++) {
if (!seen.count(candidate)) return candidate;
}
return n + 1;
}
public int firstMissingPositive(int[] nums) {
Set<Integer> seen = new HashSet<>();
for (int x : nums) seen.add(x);
int n = nums.length;
for (int candidate = 1; candidate <= n; candidate++) {
if (!seen.contains(candidate)) return candidate;
}
return n + 1;
}
def first_missing_positive(nums):
seen = set(nums)
n = len(nums)
for candidate in range(1, n + 1):
if candidate not in seen: return candidate
return n + 1
function firstMissingPositive(nums: number[]): number {
const seen = new Set<number>(nums);
for (let candidate = 1; candidate <= nums.length; candidate++) {
if (!seen.has(candidate)) return candidate;
}
return nums.length + 1;
}
int firstMissingPositive(vector<int>& nums) {
int n = nums.size();
for (int i = 0; i < n; i++) {
while (nums[i] >= 1 && nums[i] <= n && nums[nums[i] - 1] != nums[i]) {
int j = nums[i] - 1;
swap(nums[i], nums[j]);
}
}
for (int i = 0; i < n; i++) {
if (nums[i] != i + 1) return i + 1;
}
return n + 1;
}
public int firstMissingPositive(int[] nums) {
int n = nums.length;
for (int i = 0; i < n; i++) {
while (nums[i] >= 1 && nums[i] <= n && nums[nums[i] - 1] != nums[i]) {
int j = nums[i] - 1;
int tmp = nums[i]; nums[i] = nums[j]; nums[j] = tmp;
}
}
for (int i = 0; i < n; i++) {
if (nums[i] != i + 1) return i + 1;
}
return n + 1;
}
def first_missing_positive(nums):
n = len(nums)
for i in range(n):
while 1 <= nums[i] <= n and nums[nums[i] - 1] != nums[i]:
j = nums[i] - 1
nums[i], nums[j] = nums[j], nums[i]
for i in range(n):
if nums[i] != i + 1: return i + 1
return n + 1
function firstMissingPositive(nums: number[]): number {
const n = nums.length;
for (let i = 0; i < n; i++) {
while (nums[i] >= 1 && nums[i] <= n && nums[nums[i] - 1] !== nums[i]) {
const j = nums[i] - 1;
[nums[i], nums[j]] = [nums[j], nums[i]];
}
}
for (let i = 0; i < n; i++) {
if (nums[i] !== i + 1) return i + 1;
}
return n + 1;
}