How To Count Occurrences Of Each Character In String In Java?



import java.util.HashMap;

public class CountCharInStringJavaInHouse
{
  
   public static void main(String[] args)
    {
       countCharacter("How many number each character is there in string using hashmap");
    }
    static void countCharacter(String inputString)
    {
        
        HashMap<Character, Integer> resultMap = new HashMap<Character, Integer>();
        
        char[] strArray = inputString.toCharArray();

        for (char c : strArray)
        {
          
          if(resultMap.containsKey(c) )
            {

                resultMap.put(c, resultMap.get(c)+1);
            }
            else
            {


                resultMap.put(c, 1);
            }
          
        }

        System.out.println(resultMap);
    }
   
}

OutPut:
Occurrences Of Each Character In String

How to remove duplicate elements from a String in Java

Hello coders, as you can see that from the past poster writing  about only the coding example of Java program it is because we are now focusing make a proper list of Java program which will help   you're the readers to crack Java interview questions. so the next post is about most popular question  which  is asked  in the JAVA interviews , Is how to remove duplicate elements from a String  in Java.If you have not gone through the previous Java programs and also find  them here.


So in this program which is am about to write we are dealing only with the string that contains alphabets not  number .  We have a string  as ”pdbertasd”  the output should be as “pdbertas”.
While Giving interview interviewer can ask to use any third party library or third party API to code these kind of questions ,  so we have design the answer accordingly.





public class JavaInHouseRemoveDuplicate{


String toRemove="myteststring";

public static void main(String[] args) {


String finalString=JavaInHouseRemoveDuplicate(toRemove);
System.out.println("Final String is : " + finalString);

public static String JavaInHouseRemoveDuplicate(String str) {
    char[] tempStringString = str.toCharArray();
    int length = tempString.length;
    for (int i = 0; i < length; i++) {
        for (int j = i + 1; j < length; j++) {
            if (tempString[i] == tempString[j]) {
                int test = j;
                for (int k = j + 1; k < length; k++) {
                    tempString[test] = tempString[k];
                    test++;
                }
                length--;
                j--;
            }
        }
    }
    return String.copyValueOf(tempString).substring(0, length);
}

}
}


OutPut is :

Final String is :mytesring




if you have any questions or suggestions please comment below.

Program for Shifting character in a String eg. String "abcde" should be printed as "eabcd"

Shift Character Java Program



import java.util.Scanner;

public class Test
{
  public static void main(String []args) {

   Scanner in = new Scanner(System.in);
 
         System.out.println("Enter a string to shift characters");
      String  test = in.nextLine();
   
         char abc[]=test.toCharArray();
   
         char ch=abc[0];
         char chi=abc[abc.length-1];
         abc[0]=chi;
         for (int i=0;i<abc.length-1;i++){
          chi=abc[i+1];
 
          abc[i+1]=ch;
          ch=chi;
         }
     
         String result=new String(abc);

        System.out.println(result);
     
     }

}