Find Duplicate Characters in String Using Map And Count Those Characters In Java

Share:
Find Duplicate Characters in String Using Map And Count Those Characters In Java
Hello Readers In This Article We Are Going To Learn How We Can Find Duplicate Characters In The Given String And Count Those Characters And Print Them Using Map In Java.
Find Duplicate Characters in String Using Map And Count Those Characters In Java

For Example We Have a String As "Mateenn" And I Want To Check Which Characters Are Repeated More Than 1 Times In The Given String And Print Them. In This Case Output Should Be : 
e is present 2 times in "Mateenn"
n is present 2 times in "Mateenn".

So Lets Begin...
First Create a Java Project And Create a Class With Main Method In Your Favorite IDE(I am using Eclipse).

My Class Name is Finding_Duplicate_Characters.java
import java.util.*;
public class Finding_Duplicate_Characters {
    
     public static void main(String[] args){
     String str="Mateenn";
     HashMap<Character,Integer> map=new HashMap<>();
      
     for(int i=0;i<str.length();i++){
           if(map.containsKey(str.charAt(i))){
                map.put(str.charAt(i),map.get(str.charAt(i))+1);
           }
           else {
                map.put(str.charAt(i),1);
           }
     }
      
     Set<Character> keysset=map.keySet();
     Iterator<Character> itr=keysset.iterator();
     while(itr.hasNext()){
           char mykeys=itr.next();
           if(map.get(mykeys)>1)
           {
            System.out.println(mykeys+" is present "+map.get(mykeys)+" times in "+str);
           }
          
            
     }//end of while
     }//main ends here
}//end of class

OutPut :
e is present 2 times in Mateenn
n is present 2 times in Mateenn

Explanation :
-In Main Method Of Our Class We Have a String "Mateenn" And a HashMap.
-Fisrt We Are Iterating Over Given String Using for Loop.
-I Am Using Characters As a Key In Our Map And 1 As a Values.
-While Iterating Over String I Am Checking That, Is This HashMap Contain a Particular Character Using if else Using containsKey(str.charAt(i)) Method Of Map, If Yes Then Put This Character In Map As Key And Value As map.get(str.charAt(i)+1), As map.get(key) method will return value of key means map.get(key), here key is str.charAt(i) and we are adding +1 means map.get(str.charAt(i)+1will return 2 And Will Put This Value i.e 2. 

-Note :  Map can contain only unique keys but can have duplicate values. If we try to put same key which is already exist then it will replace the present key and value with new key and value.

-In This Example Control Will Go To else Part First As At The Initial Stage Map Is Empty. Ok

-So First It Will Put M as Key And 1 As Value Using else Part As Map does not contain M.

-In The Next Loop It Will Put a as Key And 1 As Value Using else Part As Map does not contain a.

-In The Next Loop It Will Put e as Key And 1 As Value Using else Part As Map does not contain e.

-Now In The Next Loop Control Will Come To If Part As Map Contain e Key And It Will Replace The e Key And Its Value With New Key i.e, e And New Value i.e., 2.

-Similarly For The n okk.

-So Now Map Would Be Like {a=1, t=1, e=2, M=1, n=2}

String str="Mateenn";
     HashMap<Character,Integer> map=new HashMap<>();
      
     for(int i=0;i<str.length();i++){
           if(map.containsKey(str.charAt(i))){
                map.put(str.charAt(i),map.get(str.charAt(i))+1);
           }
           else {
                map.put(str.charAt(i),1);
           }
     }



-Now Searching For Keys Who Has Values More Than 1.
-As We Know We Can Access The Values Of Map By Using Its Keys(map.get(key) will return its value).
-So Here I Am Getting keySet From Map Using keySet() Method Of Map And Storing This Keys In Set As We Know To Iterate Over Map We Need To Convert It To Set First.
-Iterating Over Set Of Keys Using While Loop And Checking The Keys Who Has Values More Than 1 means 2. 
Set<Character> keysset=map.keySet();
     Iterator<Character> itr=keysset.iterator();
     while(itr.hasNext()){
           char mykeys=itr.next();
           if(map.get(mykeys)>1)
           {
            System.out.println(mykeys+" is present "+map.get(mykeys)+" times in "+str);
           }

Output :
e is present 2 times in Mateenn
n is present 2 times in Mateenn
Hope It Will Help You...

Advertisement

No comments

You May Like Also

Programming Knowledge


Java JDK
Reversing String in Java
Java Generic Concept With Simple Example
Java Generic Objects Behavior In Non-Generic Area
Creating Connection With MySql Databse Using Java Application
Performing Database Operations Java MySql Insert Update Delete Select
Java Inserting Data in MySql Databse Table
Java Basic Login System Using MySql Database Table
JSP Login System Using MySql Databse
Jsp Servlet Login And Logout System Using Mysql Database Table
What is jdk in Java?
Exaplain inheritance in Java with example?
Exaplain polymorphism in Java with example?
Exaplain abstraction in Java with example?
Exaplain encapsulation in Java with example?
What is the use of this keyword in Java?
Explain ArrayList With Example
Explain LinkedList With Example
Explain Vector Class With Example
Explain Vector Class With Example
Generics in java with example
Behavior of generic objects
How can i pass generic class to a method in java
How to create a login system in jsp using mysql database?
How to create a login and logout system in jsp & servlet using mysql database?
How to connection a simple Java application with mysql database?
How to insert values in mysql databse usiong Java applications?
Perform insertion, retrieval, updatation and delete operations?
How to create a simple login system in java using mysql database?

PHP Login System Using MySql Database
PHP Complete Login System With Session And Logout Using MySql Database