Results 1 to 3 of 3
- 02-16-2008, 05:18 AM #1
Member
- Join Date
- Feb 2008
- Posts
- 3
- Rep Power
- 0
Recursive Method ==> find how many times a value is repeated in an array
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:
Java Code: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)); } }
- 02-16-2008, 05:59 AM #2
Member
- Join Date
- Feb 2008
- Posts
- 3
- Rep Power
- 0
Follow Up
I've actually altered my code already, but it still does not work. I changed the static method 'getNumberEqual()' to this:
public static int getNumberEqual(int[] data, int first, int last, int val) {
if(first==last)
return count;
if(first==val)
{
count++;
return getNumberEqual(data, first+1, last, val);
}
else
return getNumberEqual(data, first+1, last, val);
return count;
}
Thanks again:)
- 02-16-2008, 08:52 PM #3
Member
- Join Date
- Dec 2007
- Posts
- 21
- Rep Power
- 0
Java Code://import java.io.*; public class temp1class{ public static int getNumberEqual(int[] data, int curr, int val) { if (curr == 0){ if (val == data[curr]){ return 1; } else { return 0; } } else { if (val == data[curr]){ curr--; return getNumberEqual(data, curr, val)+1; } else { curr--; return getNumberEqual(data, curr, val); } } } public static void main(String[] args) { 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] = 20; ints[8] = 98; ints[9] = 1; int f = 4; int val = 20; System.out.println("GetNumberEqual for val " + val + " is "+ getNumberEqual(ints, ints.length-1, val)); } }
Similar Threads
-
exercise of recursive method
By amexudo in forum New To JavaReplies: 2Last Post: 03-09-2008, 05:55 PM -
Recursive Method ==> find minimum value from array
By NatNat in forum New To JavaReplies: 1Last Post: 02-16-2008, 09:10 PM -
Recursive Method
By bluegreen7hi in forum New To JavaReplies: 5Last Post: 11-29-2007, 04:45 AM -
problems to find the main method
By christina in forum EclipseReplies: 2Last Post: 08-06-2007, 07:51 PM -
Problems with Find method in EJB
By Nick15 in forum Enterprise JavaBeans (EJB)Replies: 0Last Post: 05-14-2007, 01:29 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks