-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPairsAndDuplicates.java
More file actions
64 lines (60 loc) · 1.79 KB
/
PairsAndDuplicates.java
File metadata and controls
64 lines (60 loc) · 1.79 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
public class PairsAndDuplicates {
/**
*
* @author sofie budman
* period 5
*/
public int getNumPairs(int[] array){
int c = 0;
for(int i = 0; i < array.length -1 ; i ++ ){
if(array[i] == array[i+1]){
c ++;
i ++;
}
}
return c;
}
public String getMostOccurElement(String[] array){
int most = 0;
String out = array[0];
for(int i = 0; i < array.length; i++){
int count = 0;
for(int j = i; j < array.length; j ++){
if(array[i].equals(array[j])){
count ++;
}
}
if(count > most){
most = count;
out = array[i];
}
}
return out;
}
public String getMostOccurCharacter(String myString){
String out = myString.substring(0,1);
int most = 0;
for(int i = 0; i < myString.length() -1 ; i ++){
int count = 0;
for(int j = i; j < myString.length() -1 ; j++){
if(myString.substring(i,i+1).equals(myString.substring(j, j+1))){
count ++;
}
}
if(count > most){
most = count;
out = myString.substring(i, i+1);
}
}
return out;
}
public static void main(String[] args){
PairsAndDuplicates p = new PairsAndDuplicates();
int[] c = {1,1,2,2,3};
String[] a = {"cat", "dog", "cat", "fish", "dog", "dog"};
String b = "aaabbbbbbbbbccccccdddddddddddddddddddd";
System.out.println(p.getMostOccurElement(a));
System.out.println(p.getMostOccurCharacter(b));
System.out.println(p.getNumPairs(c));
}
}