This is from LeetCode:
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.
Note: Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?
def missing_number(nums) nums.empty? ? 0 : nums.inject(:^)^(1..nums.length).inject(:^) end