Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 37a9a65

Browse files
author
Shuo
authored
Merge pull request #778 from openset/develop
A: Find Lucky Integer in an Array
2 parents 98b539c + f730810 commit 37a9a65

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package problem1394
2+
3+
func findLucky(arr []int) int {
4+
m := make(map[int]int)
5+
for _, v := range arr {
6+
m[v]++
7+
}
8+
ans, max := -1, 0
9+
for k, v := range m {
10+
if k > max && k == v {
11+
ans, max = k, v
12+
}
13+
}
14+
return ans
15+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package problem1394
2+
3+
import "testing"
4+
5+
type testType struct {
6+
in []int
7+
want int
8+
}
9+
10+
func TestFindLucky(t *testing.T) {
11+
tests := [...]testType{
12+
{
13+
in: []int{2, 2, 3, 4},
14+
want: 2,
15+
},
16+
{
17+
in: []int{1, 2, 2, 3, 3, 3},
18+
want: 3,
19+
},
20+
{
21+
in: []int{2, 2, 2, 3, 3},
22+
want: -1,
23+
},
24+
{
25+
in: []int{5},
26+
want: -1,
27+
},
28+
{
29+
in: []int{7, 7, 7, 7, 7, 7, 7},
30+
want: 7,
31+
},
32+
}
33+
for _, tt := range tests {
34+
got := findLucky(tt.in)
35+
if got != tt.want {
36+
t.Fatalf("in: %v, got: %v, want: %v", tt.in, got, tt.want)
37+
}
38+
}
39+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /