-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution448.go
More file actions
39 lines (34 loc) · 878 Bytes
/
solution448.go
File metadata and controls
39 lines (34 loc) · 878 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package solution448
// ============================================================================
// 448. Find All Numbers Disappeared in an Array
// URL: https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/
// ============================================================================
/*
$ go test -bench=. -benchmem
goos: linux
goarch: amd64
cpu: 13th Gen Intel(R) Core(TM) i7-13700K
Benchmark_find-24 7476871 156.5 ns/op 24 B/op 2 allocs/op
*/
func findDisappearedNumbers(nums []int) []int {
stack := make([]int, 0)
freq := make(map[int]int)
val := 0
maxval := len(nums)
for i := 0; i < maxval; i++ {
val = nums[i]
_, ok := freq[val]
if !ok {
freq[val] = 1
} else {
freq[val]++
}
}
for i := 1; i <= maxval; i++ {
_, ok := freq[i]
if !ok {
stack = append(stack, i)
}
}
return stack
}