I have written the following code and got the output. import java.util.ArrayList;
public class DuplicatesinArrayList {
public static void main(String[] args) {
ArrayList <String> al = new ArrayList<>();
al.add("Soumen");
al.add("Rahul");
al.add("Soumen");
al.add("Ram");
al.add("Rahul");
al.add("Rahul");
al.add("Rahul");
al.add("Ram");
al.add("Ram");
al.add("Ram");
al.add("Ram");
for (int i = 0; i < al.size(); i++) {
int ctr = 0;
for (int j = 0; j < al.size(); j++) {
if(al.get(i).equals(al.get(j)))
ctr++;
}
System.out.println(al.get(i)+" present " + ctr + " times");
}
}
}
The result is as follows:
Soumen present 2 times
Rahul present 4 times
Soumen present 2 times
Ram present 5 times
Rahul present 4 times
Rahul present 4 times
Rahul present 4 times
Ram present 5 times
Ram present 5 times
Ram present 5 times
Ram present 5 times
I want for each word there should be one result. Can anyone help?