Menu Bar

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Tuesday, 25 October 2016

Java Program to find Duplicate Character In String ?

Previously We have discussed The program for

Here we learn the Program for counting the Duplicate character from String using HashMap class and Set Interface.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class DuplicateCharacterFromString {

  public static void method(String str){
   Map<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> set = map.keySet();
   for(Character c : set){
     if(map.get(c) > 1){
      System.out.println("Duplicate Character : "+c+" Occurence "+map.get(c));
     }
   }
  }
  public static void main(String[] args) {
   DuplicateCharacterFromString.method("abca");
  }

}

Output:

Duplicate Character : a Occurence 2




      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Share this Blog with yours Friends !!

No comments:

Post a Comment