Hi, I need to use a recursive method to find the number of times a certain value is repeated within a specific array of integers. I'm really lost on this one. I know how to do this iteratively, but if someone can hint me towards the base case or more of the logic than that, it would be greatly appreciated.
I am aware that this can be done without using recursion, however, I need to do this recursively. Any help you can give would be greatly appreciated. Thanks in advance! My code (or what I have of code) is posted below:
import.java.io.*;
public class Main(
public static int getNumberEqual(int[] data, int first, int last, int val) {
if(first==val)
++count;
else
return getNumberEqual(data, first+1, last, val);
if(last==val)
++count;
}
public static void main(String[] args) {
int firstIndex, lastIndex;
int[] ints = new int[10];
ints[0] = 20;
ints[1] = 18;
ints[2] = 40;
ints[3] = 34;
ints[4] = 50;
ints[5] = 40;
ints[6] = 20;
ints[7] = 13;
ints[8] = 98;
ints[9] = 1;
int f = 4;
int val = 20;
firstIndex = 0;
lastIndex = ints.length - 1;
System.out.println("GetNumberEqual for val " + val + " is "+ getNumberEqual(ints, firstIndex, lastIndex, val));
}
}