http://www.technicalpage.net/search/label/SQL

Java-Interview coding problems and solutions Set3

 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 

15. Find vowels present in the given string 

16. Reverse each word in a sentence, such as: There are 365 days in a year.

will be :  [erehT, era, 563, syad, ni, a, .raey]

17. Factorial of a given value 

18. Check if the given string is Anagrams or not.  

 

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 values of A and Z are : 65, 90
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
 
15. Find vowels present in the given string

     public static void vowels() {

 

           String str = "aeAvdwiQtq@d5os6";

           ArrayList<String> vowels = new ArrayList<String>();

           ArrayList<String> vowelsInTheString = new ArrayList<String>();

           ArrayList<String> notVowels = new ArrayList<String>();

           String vowel = "aeiouAEIOU";

           for(int i=0; i<vowel.length();i++) {

                String vowelStr = Character.toString(vowel.charAt(i));

                vowels.add(vowelStr);

           }

//         OR replace above for loop by below commented lines

//         vowels.add("a");

//         vowels.add("e");

//         vowels.add("i");

//         vowels.add("o");

//         vowels.add("u");

//         vowels.add("A");

//         vowels.add("E");

//         vowels.add("I");

//         vowels.add("O");

//         vowels.add("U");

           System.out.println("vowels : "+vowels);

           for(int i=0; i<str.length();i++) {

                String str1 = Character.toString(str.charAt(i));

                if(vowels.contains(str1)) {

                      vowelsInTheString.add(str1);

                } else {

                      notVowels.add(str1);

                }

           }

           System.out.println("Vowels present in the string : "+vowelsInTheString);

           System.out.println("These are not vowels : "+notVowels);

     }

 

Output:

vowels : [a, e, i, o, u, A, E, I, O, U]

Vowels present in the string : [a, e, A, i, o]

These are not vowels : [v, d, w, Q, t, q, @, d, 5, s, 6]

 

 

16. Reverse each word in a sentence, such as: There are 365 days in a year.

will be :  [erehT, era, 563, syad, ni, a, .raey]

    public static void main(String[] Args) {

    Scanner scr = new Scanner(System.in);

    System.out.println("Enter the text");

    String str = scr.nextLine();

    String[] strArr = str.split(" ");

    int len = strArr.length;

    String[] strArrRev = new String[len];

 

    for(int i=0;i<len;i++) {

        int leni=strArr[i].length();

        String strj = "";

        for(int j=leni-1;j>=0;j--) {

            strj = strj+ strArr[i].charAt(j);

        }

        System.out.println("strj = "+strj);

        strArrRev[i]=strj;

    }

    System.out.println("Reverse Array = "+Arrays.toString(strArrRev));

    }

 17. Factorial of a given value 

       public static void factorial() {

             int n = 5;

             int multiply = 1;

             for(int i=1;i<=n;i++) {

                    multiply = multiply*i;

                   

             }

             System.out.println("factorial value = "+multiply);

     }

 Output:
factorial value = 120 

18. Check if the given string is Anagrams or not.  

public static void anagrams () {

             String str1 = "today's Sunday.";

             String str2 = "sud&atos y'ny @ad.";

             String str11 = str1.replaceAll("[^a-zA-Z]", "");

             String str22 = str2.replaceAll("[^a-zA-Z]", "");

             String[] strArr1 = new String[str11.length()];

             String[] strArr2 = new String[str22.length()];

             System.out.println("array 1 "+str11);

             System.out.println("array 2 "+str22);

             for(int i=0;i<str11.length();i++) {

                    String star1 =  Character.toString(str11.charAt(i));

                    star1 =  star1.toLowerCase();

                    if(!(star1.equals(""))){

                    strArr1[i] = star1;

                    }

             }

             for(int i=0;i<str22.length();i++) {

                    String star2 = Character.toString(str22.charAt(i));

                    star2 =  star2.toLowerCase();

                    if(!(star2.equals(""))){

                    strArr2[i] = star2;

                    }

             }

             System.out.println("array 1 "+Arrays.toString(strArr1));

             System.out.println("array 2 "+Arrays.toString(strArr2));

             Arrays.sort(strArr1);

             Arrays.sort(strArr2);

             System.out.println("sorted array 1 "+Arrays.toString(strArr1));

             System.out.println("sorted array 2 "+Arrays.toString(strArr2));

            

             //if(strArr1.equals(strArr2)) {   This is wrong for Arrays

             if(Arrays.equals(strArr1, strArr2)) {

                    System.out.println("The given string is anagrams");

             } else {

                    System.out.println("The given string is NOT anagrams ");

             }

            

       }

 

Output:

array 1 todaysSunday

array 2 sudatosynyad

array 1 [t, o, d, a, y, s, s, u, n, d, a, y]

array 2 [s, u, d, a, t, o, s, y, n, y, a, d]

sorted array 1 [a, a, d, d, n, o, s, s, t, u, y, y]

sorted array 2 [a, a, d, d, n, o, s, s, t, u, y, y]

The given string is anagrams