Search missing element from array

Identify and return the missing element from integer array contains distinct values in the range from 1 to n.

Hint

Arrange the numbers in ascending order. Go through each number in the sorted list. If a number is not at the expected position (e.g., the first number should be 1, the second number should be 2, and so on), mark the missing number. If a missing number is found, the output is the first missing number. If no missing number is found, the output is the last number in the list plus 1.

# Python implementation
ls = [8, 2, 4, 5, 3, 7, 1]
ls.sort()

output = -1

for i in range(len(ls)):
  if ls[i] != i + 1:
    output = i + 1
    break

if output < 0:
  output = ls[-1] + 1

print(output)
// Javascript implementation
const list = [8, 2, 4, 5, 3, 7, 1];
list.sort();

let output = -1;

for (let i = 0; i < list.length; i++) {
  if (list[i] != i + 1) {
    output = i + 1;
    break;
  }
}

if (output < 0) {
  output = list[list.length - 1] + 1;
}

console.log(output);