3 ms·
Here's a solution in Go. package main import ( "fmt" ) func main() { // a is the sample array a := []int{0,2, 3, 3}
by BarkMore 15y ago
Here's a solution in Go.
package main
import (
"fmt"
)
func main() {
// a is the sample array
a := []int{0,2, 3, 3}
// Reorder values such that a[i] == i when i is not the missing value.
for i := 0; i < len(a); i++ {
j := a[i]
k := a[j]
a[j] = j
a[i] = k
}
// Look for a[i] != i
for i := 0; i < len(a); i++ {
if a[i] != i {
fmt.Println("missing:", i, "duplicate:", a[i])
break
}
}
}
This is a simple expression of a solution. The code can be improved.
- BarkMore 15y agoOops, the problem statement says that the array cannot be modified.