Question

So I've seen this question asked about for help before but I had a different question about it.

The question is: (Occurrences of a specified character in an array) Write a recursive method that finds the number of occurrences of a specified character in an array. You need to declare the following two methods. The second one is a recursive helper method.

public static int count(char[] chars, char ch)

public static int count(char[] chars, char ch, int high)

The following code works perfectly fine:

public class RecursiveCharacterOccurences {
    public static int count(char[] chars, char ch) {
        return count(chars, ch, chars.length - 1);
    }

    private static int count(char[] chars, char ch, int index) {
        if (index == -1) {
            return 0;
        }
        if (chars[index] == ch) {
            return 1 + count(chars, ch, index - 1);
        }
        return count(chars, ch, index - 1);

    }

    public static void main(String[] args) {
        char[] test = {'a', 'b', 'a', 'c', 'd', 'a', 'e', 'a', 'f', 'g', 'h', 'a', 'i', 'a'};

        System.out.println(count(test, 'a'));
    }
}

I was wondering if someone could explain why that is so much better than the following code, I had done this first before realizing it wasn't actually recursion but to me it seems like a lot less code.

public class RecursiveCharacterOccurences {
    public static int count(char[] chars, char ch) {
        return count(chars, ch, 0);
    }

    private static int count(char[] chars, char ch, int count) {
        for (char a : chars) {
            if (a == ch) {
                count++;
            }
        }
        return count;
    }

    public static void main(String[] args) {
        char[] test = {'a', 'b', 'a', 'c', 'd', 'a', 'e', 'a', 'f', 'g', 'h', 'a', 'i', 'a'};

        System.out.println(count(test, 'a'));
    }
}

Thanks!

Was it helpful?

Solution

The first code example uses recursion. The second code sample uses iteration (in the form of a for-each loop). The first is not necassarily better than the second. In your case, I'm assuming the point of the assignment is to become familiar with recursion. Personally I'd use the second one because it's a lot clearer.

OTHER TIPS

Recursion is generally better for things that don't take up much time, because the system is able to handle it better. However, a for loop is better for long term, because it is designed for that.

As Anubian Noob already pointed out: The purpose of this assignment is probably not to show a particularly useful application of recursion. In practice, you would not implement this method recursively. This may have severe drawbacks: When the array is too large, you'll receive a StackOverflowError, because the number of recursive calls is limited - try it out, maybe with 1000, 10000 and 100000 array elements.

The intention is to provide an example that is easy enough to understand, but still is a little bit more involved than the trivial examples for recursive functions (like the factorial function). It shows a very general pattern that can be applied to a wide variety of problems. Particularly, the concept of the "helper" function can be very useful. Simply said: It introduces another parameter that tells you how long the recursion must repeat.

There is a considerable amount of theoretical considerations behind recursion in general. Most examples that you'll encounter in the first programming assignments will be examples of Primitive Recursive Functions, which play an important role in computability theory.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top