Find Duplicate Characters in String Using Map And Count Those Characters In Java
Output :
Hope It Will Help You...
Advertisement
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.
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)+1) will 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);
}
e is present 2 times in Mateenn
n is present 2 times in Mateenn
No comments