Answers for "how to find how many times does an object comes in an arraylist"

0

how to find how many times does an object comes in an arraylist

List<String> arr = Arrays.asList("a", "a", "b", "a", "a");

Set<String> printed = new HashSet<>();
for (String s : arr) {
    if (printed.add(s)) // Set.add() also tells if the element was in the Set!
        System.out.println("element: " + s
            + ", count: " + Collections.frequency(arr, s));
}
Posted by: Guest on April-06-2021

Code answers related to "how to find how many times does an object comes in an arraylist"

Browse Popular Code Answers by Language