Thursday, 11 September 2014

Check duplicate character using HashMap in java

package com.swain.cell;

import java.util.HashMap;

public class DuplicateCharacter {

/**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub

        String str = "Character duplicate word example java";
        char selectedchar='C';
        char ch1[]=str.toUpperCase().toCharArray();
        HashMap<Character,Integer> hm=new HashMap<Character,Integer>();
        for(int i=0; i<ch1.length; i++)
        {
            char ch=ch1[i];
            hm.put(ch,(hm.get(ch)==null?1:hm.get(ch)+1));
        }
      
        System.out.println(selectedchar+" contents  " + hm.get(selectedchar)+ " times.");
      
      
  
}

}

No comments:

Post a Comment