forked from chicuongit913/MumTestJavaExam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisTrivalent.java
49 lines (36 loc) · 837 Bytes
/
isTrivalent.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
package mumTestPackage;
import static org.junit.jupiter.api.Assertions.*;
import java.util.Arrays;
import org.junit.jupiter.api.Test;
class MumTest28 {
@Test
void test() {
assertEquals(1, isTrivalent(new int[] {22, 19, 10, 10, 19, 22, 22, 10}));
}
int isTrivalent (int[ ] a) {
if (a == null || a.length < 3) {
return 0;
}
int[] tempArray = new int[a.length];;
int lengthArray = 0;
for (int i = 0; i < a.length; i++) {
if(!isNinArray(a[i], tempArray, lengthArray)) {
tempArray[lengthArray] = a[i];
lengthArray++;
}
}
if(lengthArray == 3)
return 1;
else return 0;
}
boolean isNinArray(int n, int[] a, int lengthArray) {
if(lengthArray == 0) {
return false;
}
for (int i = 0; i < lengthArray; i++) {
if(a[i] == n)
return true;
}
return false;
}
}