Home > Blockchain >  How to recursively count number of occurences in an array
How to recursively count number of occurences in an array

Time:12-01

I've got a task that gets an int value "n" and an Int Array as parameters and is supposed to return a boolean. The method is supposed to determine, how many "n" are in the given Array. If the number is even the method should return true, else false. If the Array has the length 0, it should return "false" aswell.

What i managed to do is :

public static boolean evenNumberOf(int n,int[]arr) {
        boolean result = false;
        System.out.println("Starting count");
        if (n < arr.length) {
            if (arr[n] == n) {
                result = true;
            } else {
            return evenNumberOf(n-1,arr);
            }
        }   
        return result;  

Im just really confused and i dont know what to do to be honest. I have really tried my best but the longer i work on this task the less i understand. Any help is appreciated and thank you in advance! :)

CodePudding user response:

Separate it into two methods:

  • The method you call initially
  • and a method that gets called recursively to count the number of ns in the array:
boolean evenNumberOf(int n, int[] arr) {
  int count = countNs(n, arr, 0);
  // Logic to choose what to return based on count and/or length of arr.
}

int countNs(int n, int[] arr, int i) {
  // Check if arr[i] is equal to n.

  // Make a recursive call to countNs for i := i   1.

  // Combine the check/recursive call result to return a value.
}

CodePudding user response:

Try

    //arr should not be empty, index and count >= 0
    public static boolean evenNumberOf(int value, int index,int[]arr, int count) {
        if(index >= arr.length) return count%2 == 0;
        if(arr[index] == value ) {
            count  ;
        }
        return evenNumberOf(value,   index, arr, count);
    }

Usage example: System.out.println(evenNumberOf(2, 0, new int[]{2,0,3,7,6,11,1,2}, 0));
(You can add an helper method evenNumberOf(int value,int[]arr))

CodePudding user response:

as Recursive Counting in an Array got closed as a duplicate I will answer it here:

Let's analyze what you did and why it's wrong

public static int countN(int n,int [] arr,int i, int count) {
    
    if (arr[i] == n) {
        System.out.println("MATCH");
        count  ;
        return count;
    } 

Here you already return the count when you get a match. You shouldn't do that because if the first number is already the same it returns 1. all you need to do is increase the count here

    else {
        System.out.println("Moving on");
        i = i   1;
        countN(n,arr,i, count);
        }

Here you do the recursion. This is good. But this also needs to be done in the case that you do get a match. And it needs to return that value. But, also this only needs to be done when you are not at the end of the array yet

    if (arr.length == i) {
        evenNumberOf(n,arr);
    }

this part doesn't make sense, because you call evenNumberOf with the exact same arguments as it started so it will result in an infinite loop. you should have returned the count here. also keep in mind that the last index of an array is length - 1

putting this together you can make:

public static int countN(int n,int [] arr,int i, int count) {
    if (arr[i] == n) {
        count  ;
    }
    if (arr.length - 1 == i) {
        return count;
    }
    return countN(n, arr, i   1, count);
}   
  • Related