-
Notifications
You must be signed in to change notification settings - Fork 376
/
duplicate_test.go
65 lines (54 loc) · 1.09 KB
/
duplicate_test.go
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
Problem:
- Given an array containing n+1 numbers taken from the range 1 to n. It has
only one duplicate number but can be repeated over time. Find that one.
Example:
- Input: []int{1, 4, 4, 3, 2}
Output: 4
Approach:
- Similar to missing number problem, can place each number on its correct
index.
- If while swapping the number with its index both the numbers being swapped
are same, we have found the duplicate.
Cost:
- O(n) time, O(1) space.
*/
package gtci
import (
"testing"
"github.com/hoanhan101/algo/common"
)
func TestFindDuplicate(t *testing.T) {
tests := []struct {
in []int
expected int
}{
{[]int{}, 0},
{[]int{1, 4, 4, 3, 2}, 4},
{[]int{2, 4, 1, 4, 4}, 4},
{[]int{2, 1, 3, 3, 5, 4}, 3},
}
for _, tt := range tests {
common.Equal(
t,
tt.expected,
findDuplicate(tt.in),
)
}
}
func findDuplicate(nums []int) int {
i := 0
for i < len(nums) {
if nums[i] != i+1 {
correctIndex := nums[i] - 1
if nums[i] != nums[correctIndex] {
common.Swap(nums, i, correctIndex)
} else {
return nums[i]
}
} else {
i++
}
}
return 0
}