448. Find All Numbers Disappeared in an Array

Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.

Find all the elements of [1, n] inclusive that do not appear in this array.

Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.

Example:

Input:
[4,3,2,7,8,2,3,1]

Output:
[5,6]

Rust Solution

struct Solution;

impl Solution {
    fn find_disappeared_numbers(mut nums: Vec<i32>) -> Vec<i32> {
        let n = nums.len();
        for i in 0..n {
            let index: usize = (nums[i].abs() as usize) - 1;
            nums[index] = -nums[index].abs();
        }
        let mut res: Vec<i32> = vec![];
        for i in 1..=n {
            let index = i - 1;
            if nums[index] > 0 {
                res.push(i as i32);
            }
        }
        res
    }
}

#[test]
fn test() {
    let nums = vec![4, 3, 2, 7, 8, 2, 3, 1];
    assert_eq!(Solution::find_disappeared_numbers(nums), vec![5, 6]);
}

Having problems with this solution? Click here to submit an issue on github.