-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathArrayUtility.java
More file actions
71 lines (61 loc) · 2.05 KB
/
ArrayUtility.java
File metadata and controls
71 lines (61 loc) · 2.05 KB
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
package com.dtcc.exams.part2;
import java.util.Arrays;
public class ArrayUtility {
public Integer[] merge(Integer[] array1, Integer[] array2) {
Integer[] temp = new Integer[(array1.length + array2.length)];
System.arraycopy(array1, 0, temp, 0, array1.length);
System.arraycopy(array2, 0, temp, array1.length, array2.length);
return temp;
}
public Integer[] rotate(Integer[] array, Integer index) {
int j;
for(int i = 0; i < index; i++){
int first_val;
first_val = array[0];
for(j = 0; j < array.length-1; j++){
//Shift element of array by one
array[j] = array[j+1];
}
//First element of array will be added to the end
array[j] = first_val;
}
return array;
}
public Integer countOccurrence(Integer[] array1, Integer[] array2, Integer valueToEvaluate) {
int occurances = 0;
Integer[] temp = new Integer[(array1.length + array2.length)];
System.arraycopy(array1, 0, temp,0,array1.length);
System.arraycopy(array2,0,temp,array1.length, array2.length);
//Cycle through entire array
for(int i = 0; i < temp.length; i++) {
if(valueToEvaluate == temp[i]){
occurances+=1;
}
}
return occurances;
}
public Integer mostCommon(Integer[] array) {
Arrays.sort(array);
int most = 1;
//Most common value, start with array[0];
int val = array[0];
int current = 1;
for (int i = 1; i < array.length; i++) {
if (array[i] == array[i - 1]){
current++;
}else {
if (current > most) {
most = current;
val = array[i - 1];
}
current = 1;
}
}
// If last element is most frequent
if (current > most) {
most = current;
val = array[array.length - 1];
}
return val;
}
}