What is the Complexity of the Java’s in-built function Collections.frequency(list, element)?

We Are Going To Discuss About What is the Complexity of the Java’s in-built function Collections.frequency(list, element)?. So lets Start this Java Article.

What is the Complexity of the Java’s in-built function Collections.frequency(list, element)?

  1. What is the Complexity of the Java's in-built function Collections.frequency(list, element)?

    Collections.frequency has the following implementation (in Java 9):
    public static int frequency(Collection<?> c, Object o) { int result = 0; if (o == null) { for (Object e : c) if (e == null) result++;

  2. What is the Complexity of the Java's in-built function Collections.frequency(list, element)?

    Collections.frequency has the following implementation (in Java 9):
    public static int frequency(Collection<?> c, Object o) { int result = 0; if (o == null) { for (Object e : c) if (e == null) result++;

Solution 1

Collections.frequency has the following implementation (in Java 9):

public static int frequency(Collection<?> c, Object o) {
    int result = 0;
    if (o == null) {
        for (Object e : c)
            if (e == null)
                result++;
    } else {
        for (Object e : c)
            if (o.equals(e))
                result++;
    }
    return result;
}

So it’s O(n).

Original Author fps Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment