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 9004561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:55:26+00:00 2026-06-16T00:55:26+00:00

Basically, I have an array containing 25 different people, I need to select 5

  • 0

Basically, I have an array containing 25 different people, I need to select 5 of these people and have every single combination possible, without using duplicates of the same person.

The only logical way I can think of doing it is by having 5 for loops and checking if person has already been used, although this seems like there’s probably a better method involving recursion.

If anyone can help I’d be very appreciated.

Here’s an example of my class;

public class Calculator {

    final Person[] people = new Person[25]; //Pretend we have filled in this info already

    public List<Group> generateList()
    {
        final List<Group> possible = new ArrayList<>();
        for (int a = 0; a < 25; a++)
        {
            for (int b = 0; b < 25; b++)
            {
                for (int c = 0; c < 25; c++)
                {
                    for (int d = 0; d < 25; d++)
                    {
                        for (int e = 0; e < 25; e++)
                        {
                            final Group next = new Group();
                            next.set = new Person[] {
                                people[a],
                                people[b],
                                people[c],
                                people[d],
                                people[e]
                            };
                            possible.add(next);
                        }
                    }
                }
            }
        }
        return possible;
    }



    class Group {

        Person[] set = new Person[5];

    }

    class Person {

        String name;
        int age;

    }

}

However I’m not sure the best way to do this and if that would even get every combination. I also know there’s no duplicate checking here, which I’d do by checking;

if(b == a) continue;

Etc.

I would appreciate any help.

  • 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-16T00:55:27+00:00Added an answer on June 16, 2026 at 12:55 am

    There are many options.

    (1)

    you can improve your algoritghm by using

    for a = 0 to 25 
      for b = a+1 to 25  // use ascending-order rule 
        for c = b+1 to 25
    

    etc – this eliminates duplicate checking, taking advantage of the factorial nature of the problem

    (2)

    you can alternatively implement these as a single for loop over the whole N^R items (if you chose R items from N), and discard permutations that are not in full ascending order. This is good if you don’t know R beforehand. Imagine you are counting in base N

    for i = 0 to N^R // count in base N
      for digit = 0 to R 
        value[digit] = (i/N^digit) mod (N^(digit+1)) // extract the required digit
        if digit>0 && value[digit]<value[digit-1], SKIP  
    

    In other words, you count sequentially on these R indexes.

    (3)

    The final option, which is longer to code but more efficient for large R and N, is to use a set of indices:

    // i is an array size R, with items ranging from 0 to N
    i = int[]{ 0, 1, 2, 3, 4 }; // each is an index of the items in N
    
    while !finished
        j=0; // index to start incrementing at
        i[j] ++;
    

    then if you go above N on any index, increase j, increment i[j], and set all the i[k<j] to [0 1 2 ... j-1], and start counting again!
    this cycles most efficiently through all combinations.

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

Sidebar

Related Questions

Basically I have this string $str=word1 word2 word3; I need array( 'word1', 'word2', 'word3'
I have a Mutable Array containing different objects such as strings, UIImage , etc.
Is it possible to have selective queries in PostgreSQL which select different tables/columns based
Basically I have and array of integers that vary in size. I need to
I have a pretty simple problem. Basically I have an array called $list that
I am learning about arrays, and basically I have an array that collects a
So basically I have a huge array of arrays (only a 2-d array)... My
Basically, I have an array like this: val base_length = Array( 0, 1, 2,
Basically, I have an array of keywords, and a piece of text. I am
Basically, I have an array of strings to search for. If it were only

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.