Find Missing elements in Array – Python

The task is to identify the missing elements in a list of integers containing N-1 elements, where the integers range from 1 to N. For instance, given the list [1, 2, 4, 5], the missing number is [3].

arr = [1, 2, 3, 4, 5, 7, 6, 10]
full_range = set(range(min(arr), max(arr) + 1))
missing = list(full_range - set(arr))
missing.sort() 
print(missing)

In this above implementation, we are using set and list to find missing elements from an array. Here is a detailed breakthrough of the code.

  1. Define an array of integers.
  2. Create a full range of integers from the minimum to the maximum value found in the array.
  3. Identify the missing integers by calculating the difference between the full range and the original array.
  4. Sort the missing integers for better readability.
  5. Print the sorted list of missing integers.