how to identify duplicate values in arraylist

Answer Posted / srikanth m

List<String> list = new ArrayList<String>();
list.add("abc");
list.add("def");
list.add("abc");




HashMap<String, Integer> map = new HashMap<String, Integer>();




for (String str : list) {
Integer c = (Integer) map.get(str);
if (c == null || c == 0) {
map.put(str, 1);
} else {
map.put(str, ++c);
}
}
System.out.println(map);

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is downcasting?

793


What is a default constructor and also define copy contrucyor?

847


In a program, initializing an array of 100 KB is throwing an out of memory exception while there is 100 MB of memory available. Why?

773


what is synchronization? : Java thread

781


What happens when I use / and % with a negative numerator?

774


What is a lambda expression ? What's its use ?

808


Is arraylist zero based?

773


How can you avoid serialization in child class if the base class is implementing the serializable interface?

836


What is included in core java?

813


Outline the major features of java.

802


What is a private class in java?

730


What is java util?

792


Is zero a positive integer?

771


we have syntax like for(int var : arrayName) this syntax is to find whether a number is in the array or not.but i want to know how to find that number's location.

1805


What is the advantage of OOP in java?

881