Would anyone know the most efficient way to look for duplicates in a String ArrayList and print out the duplicates?
For example I have an ArrayList containing the following:
ArrayList<String> carLIST = new ArrayList<String>();
carLIST = {"Car1", "Car2", "Car3", "Car1", "Car2", "Car2"};
Basically anything in the list more than once I'm looking to find the duplicates (which I think I've done below) and also return a System.out.println(); to show the following:
Car1 : count=2
Car2 : count=3
Map<String,Integer> repeatationMap = new HashMap<String,Integer>();
for(String str : carLIST) {
if (repeatationMap.containsKey(str) {
repeatationMap.put(str,repeatationMap.get(str) +1);
}
else {
epeatationMap.put(str, 1);
}
// if (repeatationMap.get(str) > 1) {
// System.out.println(repeatationMap.entrySet());
// }
}
The code commented out is what I thought it would be to print out the duplicates but I'm seriously wrong! Have no idea how to print out the duplicate cars in the list and show its count.