RE: Efficiency is important. Here's a better way to find the length of an array.

You are viewing a single comment's thread from:

Efficiency is important. Here's a better way to find the length of an array.

in humor •  7 years ago 

Image Transcription:


[Screenshot of a function which returns the length of an array]

public static int arrayLength(int[] arr) {
    int min = 0;
    int max = Integer.MAX_VALUE;
    while (min + 1 < max) {
        int mid = (min + max) / 2;
        try {
            int elem = arr[mid];
            max = mid - 1;
        } catch (ArrayIndexOutOfBoundsException e) {
            min = mid + 1;
        }
    }
    return min;
}

^^I'm a human volunteer content transcriber for Reddit! If you'd like more information on what we do and why we do it, click here!

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!