Drop Down

Monday, February 4, 2019

Duplicate Strings

package interview;
import java.util.*;
public class DuplicateStrings
{
//String-->String.split()--->String[]--->Uppercase--->Hashmap
public static void main(String[] args)
{
  String str = "This is what is this world in world 45 67 45";
  String[] str2 = str.split(" ");
  String[] str3 = toUpperString(str2);
  show(str3);
 
  Map<String,Integer> map = new HashMap<>();
  for(String s : str3)
  {
  if(map.containsKey(s))
  {
  map.put(s,map.get(s)+1);
  }
  else
  {
  map.put(s, 1);
  }
  }
//*****************************
  for(Map.Entry<String,Integer> entry : map.entrySet())
  {
  if(entry.getValue()>1)
  {
  System.out.println(entry.getKey()+" --->"+entry.getValue());
  }
  }
 
}

    private static void show(String[] str3)
    {
  for(String s : str3)
  System.out.print(s+" ");
  System.out.println("");
    }

private static String[] toUpperString(String[] str2)
     {
for(int i =0;i<str2.length;i++)
{
str2[i] = str2[i].toUpperCase();
}
    return str2;
     }

}

No comments:

Post a Comment

Java 8 Notes Pics