-
Notifications
You must be signed in to change notification settings - Fork 1
/
Challenge01.java
80 lines (66 loc) · 2.12 KB
/
Challenge01.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package challenge01;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.LinkedHashSet;
import java.util.Set;
import org.junit.jupiter.api.Test;
/**
* The Class Challenge01.
*
* print duplicate characters from a given String,
* for example if String is "Java" then program should print "a".
* Bonus points if your program is robust and handle different kinds of input
* e.g. String without duplicate, null or empty String etc.
* Bonus points if you also write unit tests for normal and edge cases.
*/
public class Challenge01 {
/**
* Gets the duplicates.
*
* @param string the string
* @return the duplicates
*/
public Set<Character> getDuplicates(String string) {
Set<Character> toReturn = new LinkedHashSet<>();
if (string == null || string.isEmpty()) {
return toReturn;
} else {
for (int i = 0; i < string.length(); i++) {
Character c = string.charAt(i);
if (string.substring(i + 1, string.length()).contains(c.toString())) {
toReturn.add(c);
}
}
return toReturn;
}
}
@Test
public void nullTest() {
Challenge01 tester = new Challenge01();
Set<Character> s=tester.getDuplicates(null);
assertEquals(0, s.size());
}
@Test
public void emptyTest() {
Challenge01 tester = new Challenge01();
String str=new String("");
Set<Character> s=tester.getDuplicates(str);
assertEquals(0, s.size());
}
@Test
public void aTest() {
Challenge01 tester = new Challenge01();
String str=new String("Java");
Set<Character> s=tester.getDuplicates(str);
assertTrue(s.contains('a'));
}
@Test
public void bTest() {
Challenge01 tester = new Challenge01();
String str=new String("Programming");
Set<Character> s=tester.getDuplicates(str);
assertTrue(s.contains('r'));
assertTrue(s.contains('g'));
assertTrue(s.contains('m'));
}
}