|
| 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