Skip to content

Commit

Permalink
Merge 5e0cd11 into 88461c6
Browse files Browse the repository at this point in the history
  • Loading branch information
vaskoz committed Feb 2, 2019
2 parents 88461c6 + 5e0cd11 commit 5f8fbb0
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,4 @@ problems from
* [Day 161](https://github.com/vaskoz/dailycodingproblem-go/issues/334)
* [Day 162](https://github.com/vaskoz/dailycodingproblem-go/issues/335)
* [Day 163](https://github.com/vaskoz/dailycodingproblem-go/issues/336)
* [Day 164](https://github.com/vaskoz/dailycodingproblem-go/issues/340)
17 changes: 17 additions & 0 deletions day164/problem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package day164

// FindDuplicate returns the one duplicate value between
// 1 to N.
// Assumes N+1 values are provided.
func FindDuplicate(nums []int) int {
sub := len(nums) - 1
sum := 0
for _, n := range nums {
sum += n
for sum > 0 && sub > 0 {
sum -= sub
sub--
}
}
return sum
}
27 changes: 27 additions & 0 deletions day164/problem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package day164

import "testing"

var testcases = []struct {
nums []int
dup int
}{
{[]int{3, 1, 5, 4, 2, 4}, 4},
}

func TestFindDuplicate(t *testing.T) {
t.Parallel()
for _, tc := range testcases {
if dup := FindDuplicate(tc.nums); dup != tc.dup {
t.Errorf("Expected %v got %v", tc.dup, dup)
}
}
}

func BenchmarkFindDuplicate(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, tc := range testcases {
FindDuplicate(tc.nums)
}
}
}

0 comments on commit 5f8fbb0

Please sign in to comment.