Leetcode - 448. Find All Numbers Disappeared in an Array
Leetcode - 448. Find All Numbers Disappeared in an Array
- Given an array nums of n integers where nums[i] is in the range [1, n], return an array of all the integers in the range [1, n] that do not appear in nums.
Solution
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
func findDisappearedNumbers(nums []int) []int {
i := 0
for i<len(nums){
indexOfCurrentNumber := nums[i]-1
if nums[i]!=nums[indexOfCurrentNumber]{
nums[i], nums[indexOfCurrentNumber] = nums[indexOfCurrentNumber], nums[i]
}else{
i++
}
}
result := []int{}
for ind, val := range nums{
if (ind+1)!=val{
result = append(result, ind+1)
}
}
return result
}
This post is licensed under CC BY 4.0 by the author.