Skip to content

Commit 9f10a2c

Browse files
committed
Find the duplicate number
1 parent 40c247d commit 9f10a2c

File tree

2 files changed

+220
-0
lines changed

2 files changed

+220
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// 287. Find the duplicate number
2+
// Topics: 'Array', 'Two Pointers', 'Binary Search', 'Bit Manipulation'
3+
// Level: 'Medium'
4+
5+
// Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive.
6+
7+
// There is only one repeated number in nums, return this repeated number.
8+
9+
// You must solve the problem without modifying the array nums and using only constant extra space.
10+
11+
// Example 1:
12+
13+
// Input: nums = [1,3,4,2,2]
14+
// Output: 2
15+
16+
// Example 2:
17+
18+
// Input: nums = [3,1,3,4,2]
19+
// Output: 3
20+
21+
// Example 3:
22+
23+
// Input: nums = [3,3,3,3,3]
24+
// Output: 3
25+
26+
// Constraints:
27+
28+
// 1 <= n <= 105
29+
// nums.length == n + 1
30+
// 1 <= nums[i] <= n
31+
// All the integers in nums appear only once except for precisely one integer which appears two or more times.
32+
33+
package findtheduplicatenumber
34+
35+
func findDuplicate(nums []int) int {
36+
slow, fast := nums[0], nums[0]
37+
for {
38+
slow = nums[slow]
39+
fast = nums[nums[fast]]
40+
if fast == slow {
41+
break
42+
}
43+
}
44+
slow2 := nums[0]
45+
for slow2 != slow {
46+
slow = nums[slow]
47+
slow2 = nums[slow2]
48+
}
49+
return slow
50+
}
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
package findtheduplicatenumber
2+
3+
import "testing"
4+
5+
func TestFindDuplicate_Example1(t *testing.T) {
6+
nums := []int{1, 3, 4, 2, 2}
7+
result := findDuplicate(nums)
8+
9+
if result != 2 {
10+
t.Errorf("Expected 2, got %d", result)
11+
}
12+
}
13+
14+
func TestFindDuplicate_Example2(t *testing.T) {
15+
nums := []int{3, 1, 3, 4, 2}
16+
result := findDuplicate(nums)
17+
18+
if result != 3 {
19+
t.Errorf("Expected 3, got %d", result)
20+
}
21+
}
22+
23+
func TestFindDuplicate_Example3(t *testing.T) {
24+
nums := []int{3, 3, 3, 3, 3}
25+
result := findDuplicate(nums)
26+
27+
if result != 3 {
28+
t.Errorf("Expected 3, got %d", result)
29+
}
30+
}
31+
32+
func TestFindDuplicate_TwoElements(t *testing.T) {
33+
nums := []int{1, 1}
34+
result := findDuplicate(nums)
35+
36+
if result != 1 {
37+
t.Errorf("Expected 1, got %d", result)
38+
}
39+
}
40+
41+
func TestFindDuplicate_Complex(t *testing.T) {
42+
nums := []int{2, 5, 9, 6, 9, 3, 8, 9, 7, 1}
43+
result := findDuplicate(nums)
44+
45+
if result != 9 {
46+
t.Errorf("Expected 9, got %d", result)
47+
}
48+
}
49+
50+
func TestFindDuplicate_DuplicateAtEnd(t *testing.T) {
51+
nums := []int{1, 2, 3, 4, 4}
52+
result := findDuplicate(nums)
53+
54+
if result != 4 {
55+
t.Errorf("Expected 4, got %d", result)
56+
}
57+
}
58+
59+
func TestFindDuplicate_DuplicateInMiddle(t *testing.T) {
60+
nums := []int{1, 2, 5, 3, 5, 4}
61+
result := findDuplicate(nums)
62+
63+
if result != 5 {
64+
t.Errorf("Expected 5, got %d", result)
65+
}
66+
}
67+
68+
func TestFindDuplicate_ThreeElements(t *testing.T) {
69+
nums := []int{2, 1, 2}
70+
result := findDuplicate(nums)
71+
72+
if result != 2 {
73+
t.Errorf("Expected 2, got %d", result)
74+
}
75+
}
76+
77+
func TestFindDuplicate_FourElements(t *testing.T) {
78+
nums := []int{4, 3, 1, 4, 2}
79+
result := findDuplicate(nums)
80+
81+
if result != 4 {
82+
t.Errorf("Expected 4, got %d", result)
83+
}
84+
}
85+
86+
func TestFindDuplicate_LargerArray(t *testing.T) {
87+
nums := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 5}
88+
result := findDuplicate(nums)
89+
90+
if result != 5 {
91+
t.Errorf("Expected 5, got %d", result)
92+
}
93+
}
94+
95+
func TestFindDuplicate_DuplicateOne(t *testing.T) {
96+
nums := []int{1, 1, 2, 3, 4}
97+
result := findDuplicate(nums)
98+
99+
if result != 1 {
100+
t.Errorf("Expected 1, got %d", result)
101+
}
102+
}
103+
104+
func TestFindDuplicate_MultipleDuplicates(t *testing.T) {
105+
nums := []int{2, 5, 9, 6, 9, 3, 8, 9, 7, 1, 4}
106+
result := findDuplicate(nums)
107+
108+
if result != 9 {
109+
t.Errorf("Expected 9, got %d", result)
110+
}
111+
}
112+
113+
func TestFindDuplicate_AdjacentDuplicates(t *testing.T) {
114+
nums := []int{1, 2, 3, 3, 4, 5}
115+
result := findDuplicate(nums)
116+
117+
if result != 3 {
118+
t.Errorf("Expected 3, got %d", result)
119+
}
120+
}
121+
122+
func TestFindDuplicate_NonAdjacentDuplicates(t *testing.T) {
123+
nums := []int{5, 1, 2, 3, 4, 5}
124+
result := findDuplicate(nums)
125+
126+
if result != 5 {
127+
t.Errorf("Expected 5, got %d", result)
128+
}
129+
}
130+
131+
func TestFindDuplicate_LargeN(t *testing.T) {
132+
nums := make([]int, 101)
133+
for i := 1; i <= 100; i++ {
134+
nums[i-1] = i
135+
}
136+
nums[100] = 50
137+
138+
result := findDuplicate(nums)
139+
140+
if result != 50 {
141+
t.Errorf("Expected 50, got %d", result)
142+
}
143+
}
144+
145+
func TestFindDuplicate_MaxValue(t *testing.T) {
146+
nums := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10}
147+
result := findDuplicate(nums)
148+
149+
if result != 10 {
150+
t.Errorf("Expected 10, got %d", result)
151+
}
152+
}
153+
154+
func TestFindDuplicate_SequentialWithGap(t *testing.T) {
155+
nums := []int{2, 5, 1, 3, 4, 6, 7, 3}
156+
result := findDuplicate(nums)
157+
158+
if result != 3 {
159+
t.Errorf("Expected 3, got %d", result)
160+
}
161+
}
162+
163+
func TestFindDuplicate_ReverseOrder(t *testing.T) {
164+
nums := []int{5, 4, 3, 2, 1, 3}
165+
result := findDuplicate(nums)
166+
167+
if result != 3 {
168+
t.Errorf("Expected 3, got %d", result)
169+
}
170+
}

0 commit comments

Comments
 (0)