Friday, 20 April 2018

write a program to find duplicate character in a string with number of occerences?

package stringdemo;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class FindDuplicate {

public static void findDuplicateCount(String s1)
{
Map<Character, Integer> m=new HashMap<Character,Integer>();
char[] ch = s1.toCharArray();
for(char c:ch)
{
if(m.containsKey(c))
{
m.put(c, m.get(c)+1);
}
else
{
m.put(c, 1);
}
}

Set<Character> key = m.keySet();
for(Character ct:key)
{
if(m.get(ct)>1)
{
System.out.println(ct+"------->"+m.get(ct));
}
}


}
public static void main(String[] args) {
findDuplicateCount("GovindBallabhKhan");
}
}


o/p:-
a------->3
h------->2
l------->2
n------->2

No comments:

Post a Comment