This page contains Java code for below questions:
11. Find the second most repeated char in a given string
12. Reformat the given string : "asdfsdafgde" => "2a-2s-3d-1e-2f-1g"
13. Find missing value in {1,3,4,5}, {1,3,5,9}
14. Find the uppercase chars present in a given string
11. find the second most repeated char in a given string
public void secMostRepChar() {
String str = "asdfsgdfsdasdferfvdera";
int len = str.length();
HashMap<String, Integer> map = new HashMap<>();
for(int i=0;i<len;i++) {
char ch = str.charAt(i);
String strCh = Character.toString(ch);
if(map.containsKey(strCh)) {
map.put(strCh, map.get(strCh)+1);
} else {
map.put(strCh, 1);
}
}
System.out.println("The duplicate chars are : "+map);
int mapLen = map.size();
int map2ndVal = 0;
ArrayList<Integer> alist = new ArrayList<>(map.values());
ArrayList<String> blist = new ArrayList<>(map.keySet());
for(int i=1;i<mapLen;i++) {
if((alist.get(i-1))>map2ndVal && (alist.get(i-1)<alist.get(i))) {
map2ndVal=alist.get(i-1);
}
}
System.out.println("The second highest repeated number : "+map2ndVal);
ArrayList<String> clist = new ArrayList<>();
for(int i=0;i<alist.size(); i++) {
if(alist.get(i)==map2ndVal) {
//clist.add(blist.get(i)+Integer.toString(map2ndVal)); OR clist.add((blist.get(i)).concat(Integer.toString(map2ndVal)));
}
}
System.out.println("The second highest repeated char/s : "+clist);
}
Output:
The duplicate chars are : {a=3, r=2, s=4, d=5, e=2, f=4, v=1, g=1}
The second highest repeated number : 4
The second highest repeated char/s : [s4, f4]
12. Reformat the given string
public static void reformatString() {
String str = "asdfsdafgde"; //2a-2s-3d-1e-2f-1g
int len = str.length();
HashMap<String,Integer> map = new HashMap<>();
for(int i=0;i<len;i++) {
String str1= Character.toString(str.charAt(i));
if(map.containsKey(str1)) {
map.put(str1, map.get(str1)+1);
} else {
map.put(str1, 1);
}
}
System.out.println("The char repeation is like this "+map);
ArrayList<String> alist = new ArrayList<>(map.keySet());
ArrayList<Integer> blist = new ArrayList<>(map.values());
ArrayList<String> clist = new ArrayList<>();
String strNewFormat = "";
for(int i=0;i<map.size();i++) {
String str1 = Integer.toString(blist.get(i))+alist.get(i);
clist.add(str1);
}
System.out.println("combined elements "+clist);
for(int i=0;i<map.size()-1;i++) {
//strNewFormat=strNewFormat+clist.get(i)+"-"; //OR
strNewFormat = strNewFormat.concat(clist.get(i)).concat("-");
}
strNewFormat=strNewFormat+clist.get(map.size()-1);
System.out.println("New formated string: "+strNewFormat);
}
Output:
The char repeation is like this {a=2, s=2, d=3, e=1, f=2, g=1}
combined elements [2a, 2s, 3d, 1e, 2f, 1g]
New formated string: 2a-2s-3d-1e-2f-1g
13. Find missing value in {1,3,4,5}, {1,3,5,9}
public static void missingdigit1() {
int[] numArr1 = {1,3,4,5}; //Result = 2
int[] numArr2 = {1,3,5,9}; //Result = 7
int count1 = 5;
int d1 = numArr1[1]-numArr1[0];//2
int d2= numArr1[2]-numArr1[1];//1
int d3=numArr1[3]-numArr1[2];//1
int diff = 0;
diff = (d1==d2)?d1:d3; //Ternary Operator or Conditional Operator
// OR
// if(d1==d2) {
// diff = d2;
// } else if(d1==d3) {
// diff = d1;
// } else if (d2==d3) {
// diff = d2;
// }
System.out.println("diff : "+diff);
int missing = 0;
for(int i=0; i<4;i++) {
if(!(numArr1[i]+diff==numArr1[i+1])) {
missing = numArr1[i]+diff;
break;
}
}
System.out.println("missing value : "+missing);
}
14. Find the uppercase chars present in a given string
public static void findUpperCaseChars() {
String str = "aADjlsdfeDRTsdfPasS";
int upperCaseA = 'A';
int upperCaseZ = 'Z';
System.out.println("The values of A and Z are : "+upperCaseA+", "+upperCaseZ);
for(int i=0;i<str.length();i++) {
if(str.charAt(i)>=upperCaseA && str.charAt(i)<=upperCaseZ){
System.out.println("The UpperCase Chars are : "+str.charAt(i));
}
}
}
Output:
The UpperCase Chars are : A
The UpperCase Chars are : D
The UpperCase Chars are : D
The UpperCase Chars are : R
The UpperCase Chars are : T
The UpperCase Chars are : P
The UpperCase Chars are : S