Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: how to find all occurrences of a number in an array recursivelly

  1. #1
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default how to find all occurrences of a number in an array recursivelly

    guys, given the fact I have an array how can I find all occurrences of the number recursively. I have figure out how to do a linear search recursively, but that would exit once it finds the number... what modification do I need to make in order to find all occurrences of the number.

    int[] nums = {20, 25, 29, 55, 25}
     
    //search with recursion
        public static int search(int[] a, int n, int i)
        {
            if(i == a.length-1)
                return -1;
            else if(a[i] == n)
                return i;
            else
                return search(a, n, ++i);        
        }
     
    search(nums, 25, 0);


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,659
    Thanks
    25
    Thanked 325 Times in 302 Posts

    Default Re: how to find all occurrences of a number in an array recursivelly

    Are you wanting to simply count them or return an array of all of their indexes?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: how to find all occurrences of a number in an array recursivelly

    just count them...

  4. #4
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: how to find all occurrences of a number in an array recursivelly

    thought about it for a bit, and came up with answer, may not be the most efficient. If someone got a better implementation.. post

    static int count = 0, i = 0;
        public static int displayOccu(int[] a, int n)
        {
            if(i == a.length)
                return count;
            else 
            {
                if(a[i] == n)
                {
                    i++;
                    count++;
                    return displayOccu(a, n);
                }
                else
                {
                    i++;
                    return displayOccu(a, n);
                }            
            }              
         }

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,659
    Thanks
    25
    Thanked 325 Times in 302 Posts

    Default Re: how to find all occurrences of a number in an array recursivelly

    There is a much better way of doing it, by taking the count variable out. This should also do it:
    public static int displayOccu(int[] a, int n, int i)
    {
    	if(i == a.length)
    		return 0;
    	else if(a[i] == n)
    		return 1+displayOcc(a,n,i+1);
    	else
    		return displayOcc(a,n,i+1);
    }

    This code is very similar to examples of summation with recursion. If the number is a hit, we add one and check the rest, otherwise we just check the rest. When it reaches the end of the array, it returns 0 to the previous call, and then adds together each 1 as the recursive call returns to where it was called.
    I haven't compiled or tested the code, but in theory it should work.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. Find Biggest Number in Array
    By jo15765 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 12th, 2012, 03:01 PM
  2. Find the two largest number
    By vendettabf in forum What's Wrong With My Code?
    Replies: 15
    Last Post: December 29th, 2011, 12:23 PM
  3. How to find the larget number on binary search tree
    By Jurgen in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 24th, 2011, 10:26 AM
  4. find the sum of even number not exceed four million
    By i4ba1 in forum Algorithms & Recursion
    Replies: 10
    Last Post: June 29th, 2011, 08:08 AM
  5. Find total number less than average
    By maximus20895 in forum Collections and Generics
    Replies: 2
    Last Post: December 1st, 2010, 12:46 PM