Skip to content

Commit 7c8b04d

Browse files
realDuYuanChaogithub-actions
andauthored
Leetcode 2021.3.18 (examplehub#76)
* find-lucky-integer-in-an-array * Formatted with Google Java Formatter Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent 9a05906 commit 7c8b04d

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.examplehub.leetcode.easy;
2+
3+
/** https://leetcode.com/problems/find-lucky-integer-in-an-array/ */
4+
public class FindLuckyIntegerInAnArray {
5+
6+
public static int solution1(int[] arr) {
7+
int maxLuckyNum = -1;
8+
for (int i = 0; i < arr.length; ++i) {
9+
int count = 0;
10+
for (int j = 0; j < arr.length; ++j) {
11+
if (arr[i] == arr[j]) {
12+
count++;
13+
}
14+
}
15+
if (count > 1 && count == arr[i]) {
16+
maxLuckyNum = arr[i];
17+
}
18+
}
19+
return maxLuckyNum;
20+
}
21+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.examplehub.leetcode.easy;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
class FindLuckyIntegerInAnArrayTest {
8+
@Test
9+
void testSolution1() {
10+
int[] arr = {2, 2, 3, 4};
11+
assertEquals(2, FindLuckyIntegerInAnArray.solution1(arr));
12+
13+
arr = new int[] {1, 2, 2, 3, 3, 3};
14+
assertEquals(3, FindLuckyIntegerInAnArray.solution1(arr));
15+
16+
arr = new int[] {2, 2, 2, 3, 3};
17+
assertEquals(-1, FindLuckyIntegerInAnArray.solution1(arr));
18+
19+
arr = new int[] {-1};
20+
assertEquals(-1, FindLuckyIntegerInAnArray.solution1(arr));
21+
}
22+
}

0 commit comments

Comments
 (0)