Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8286281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:41:34+00:00 2026-06-08T11:41:34+00:00

I have a object, let’s call it Friend. This object has method GetFriendsOfFriend, that

  • 0

I have a object, let’s call it “Friend”.
This object has method “GetFriendsOfFriend”, that returns a List<Friend>.

Given a user input of say 5, all of the Friends friends and the friends friends friends (you get the point) down in a level of 5 (this can be up to 20).

This may be a lot of calculations, so I don’t know if recursion is the best solution.

Does anyone have a smart idea of
1. How to do this recursive function best?
2. How to do it without recursion.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T11:41:35+00:00Added an answer on June 8, 2026 at 11:41 am

    Whilst is is certainly possible to do this without recursion, I don’t see a particular problem with what you’re trying to do. To prevent things going crazy, it might make sense to set a maximum to prevent your program from dying.

    public class Friend
    {
        public static readonly int MaxDepth = 8; // prevent more than 8 recursions
    
        private List<Friend> myFriends_ = new List<Friend>();
    
        // private implementation
        private void InternalFriends(int depth, int currDepth, List<Friend> list)
        {
            // Add "us"
            if(currDepth > 1 && !list.Contains(this))
                list.Add(this);
    
            if(currDepth <= depth)
            {
                foreach(Friend f in myFriends_)
                {
                    if(!list.Contains(f))
                        f.InternalFriends(depth, depth + 1, list); // we can all private functions here.
                }
            }
        } // eo InternalFriends
    
    
        public List<Friend> GetFriendsOfFriend(int depth)
        {
            List<Friend> ret = new List<Friend>();
            InternalFriends(depth < MaxDepth ? depth : MaxDepth, 1, ret);
            return ret;
        }  // eo getFriendsOfFriend
    } // eo class Friend
    

    EDIT: Fixed an error in the code in that an actual friend would not get added, just “their” friends. This is only necessary when adding friends after a depth of “1” (the first call). I also made use of Contains to check for duplicates.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say my domain looks like this: I have an object, Vehicle, that has
Let's say I have an object - a User object in this case -
I have a java object (let's call it Foo ) with a length() method.
Let's imagine that we have object Animal $.Animal = function(options) { this.defaults = {
Let's say I have an object MyObject that looks like this: public class MyObject
I have an object, let's call it catRancher, he has a catHerd(ArrayList), in my
I have a data object (let's say it's called 'Entry') that has a set
I have an android app with a Business Object (let's call it SuperClass) that
Let's say that I have a object that looks like this var object =
I have a method which returns me some object let say a String object.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.