Enter your E-mail Address below for Free E-mail Alerts right Into your Inbox: -

Monday

How to find out the duplicate characters in String java program

How to find out the duplicate characters in String java program

 This is one of the normal java interview questions asked for fresher and experienced java developers. To solve this jobberindia has come along with a solution. Below is the program to find the duplicate characters in a string.

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

public class checkRepeatingCharacters{

    public static void main(String args[]) {
        getDupCharacters("blogger");
    }

    
    public static void getDupCharacters(String w) {
        char[] char = w.toCharArray();

       
        Map<Character, Integer> m = new HashMap<Character, Integer>();
        for (Character c : char) {
            if (m.containsKey(ch)) {
                m.put(c, m.get(c) + 1);
            } else {
                m.put(c, 1);
            }
        }

        
        Set<Map.Entry<Character, Integer>> eS = charMap.entrySet();
        System.out.println("duplicate characters is"+word);
        for (Map.Entry<Character, Integer> e : eS) {
            if (e.getValue() > 1) {
                System.out.println("key:"+e.getKey()+"value:"e.getValue());
            }
        }
    }

}

duplicate characters is
g : 2

No comments:

Post a Comment