Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.HashMap;
- import java.util.Set;
- public class SemifinalatSchool {
- public static void main(String[] arg){
- String[] names = {"Bob", "Cindy", "Tony", "Julie", "Tony", "Julie"};
- HashMap<String, Integer> hashMap = new HashMap<String, Integer>();
- for (int i = 0; i < names.length; i++)
- hashMap.put(names[i], 1);
- if (names.length == hashMap.size())
- System.out.println("Array has unique elements");
- else
- System.out.println("Array does not have unique elements");
- Set<String> keys = hashMap.keySet();
- for (String key: keys)
- System.out.println(key);
- hashMap.clear();
- int value = 0;
- for (int i = 0; i < names.length; i++)
- if(hashMap.containsKey(names[i])){
- value = hashMap.get(names[i]).intValue();
- hashMap.put(names[i], ++value);
- }
- else
- hashMap.put(names[i], 1);
- System.out.println(hashMap);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement