Drop Down

Monday, February 4, 2019

Duplicate Char Display

package interview;

import java.util.*;

public class DuplicateCharDisplay {

public static void main(String[] args)
{
  String str = "welcome to apple world";
  displayDuplicate(str);

}

private static void displayDuplicate(String str)
{
  Map <Character,Integer> map = new HashMap<>();
  char  [] ch = str.toCharArray();
  for(char c : ch)
  {
  if(!map.containsKey(c))
  {
  map.put(c, 1);
  }
  else
  {
map.put(c, map.get(c)+1); 
  }
  }
  // Now Iterate map
  /*for(char c : map.keySet())
  {
  System.out.println(c +" - "+map.get(c));
  }*/
 
  for(Map.Entry<Character,Integer> entry : map.entrySet())
{
    if(entry.getValue()>1)
    {
System.out.printf("%s : %d %n",entry.getKey(),entry.getValue());
    }
}
}

}

No comments:

Post a Comment

Java 8 Notes Pics