Skip to content

Commit cf31fa6

Browse files
refactor 217
1 parent 9ee673f commit cf31fa6

File tree

2 files changed

+35
-14
lines changed

2 files changed

+35
-14
lines changed

src/main/java/com/fishercoder/solutions/_217.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,25 @@
55

66
/**
77
* 217. Contains Duplicate
8+
*
89
* Given an array of integers, find if the array contains any
910
* duplicates. Your function should return true if any value appears at least twice in the array,
1011
* and it should return false if every element is distinct.
1112
*/
1213
public class _217 {
13-
public boolean containsDuplicate(int[] nums) {
14-
if (nums == null || nums.length == 0) {
15-
return false;
16-
}
17-
Set<Integer> set = new HashSet();
18-
for (int i : nums) {
19-
if (!set.add(i)) {
20-
return true;
14+
public static class Solution1 {
15+
public boolean containsDuplicate(int[] nums) {
16+
if (nums == null || nums.length == 0) {
17+
return false;
18+
}
19+
Set<Integer> set = new HashSet();
20+
for (int i : nums) {
21+
if (!set.add(i)) {
22+
return true;
23+
}
2124
}
25+
return false;
2226
}
23-
return false;
2427
}
2528

26-
public static void main(String... strings) {
27-
int[] nums = new int[]{1, 2, 3, 4, 3};
28-
_217 test = new _217();
29-
System.out.println(test.containsDuplicate(nums));
30-
}
3129
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._217;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _217Test {
10+
private static _217.Solution1 solution1;
11+
private static int[] nums;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _217.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
nums = new int[]{1, 2, 3, 4, 3};
21+
assertEquals(true, solution1.containsDuplicate(nums));
22+
}
23+
}

0 commit comments

Comments
 (0)