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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:21:41+00:00 2026-05-26T10:21:41+00:00

For simplicity, let’s say that I have two sets of words, sorted into alphabetical

  • 0

For simplicity, let’s say that I have two sets of words, sorted into alphabetical order. One set starts at “aardvark” and ends at “melon”, and the other starts at “melon” and ends at “zebra”. The word “melon” appears in both sets.

If I were to take an input word, say “banana”, what would be a good (and efficient) way of determining which set of words it should belong to? Note: this isn’t a question about whether the word “banana” already exists in one set, but rather a question about how to determine which set the word should exist in.

If there is an algorithm that someone knows, great. If they can provide some version in Java, even better!

Edit: Should also point out, whilst my example only has 2 sets, I want the algorithm to work with n sets.

  • 1 1 Answer
  • 3 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-05-26T10:21:41+00:00Added an answer on May 26, 2026 at 10:21 am

    For two sets:

    If word is your word (e.g. "banana"):

    int cmp = word.compareTo("melon");
    if (cmp < 0) {
      // it belongs to the first set
    } else if (cmp > 0) {
      // it belongs to the second set
    } else {
      // the word is "melon"
    }
    

    For n sets:

    Place the dividing words into an ArrayList<String> (call it dividers) in alphabetical order:

    ArrayList<String> dividers = new ArrayList<String>();
    //... populate `dividers` ...
    Collections.sort(dividers);
    

    Now you can use Collections.binarySearch() to figure out which set the word belongs to:

    int pos = Collections.binarySearch(dividers, word);
    if (pos >= 0) {
      // the word is the divider between sets `pos` and `pos+1`
    } else {
      int num = -(pos + 1);
      // the word belong to set number `num`
    }
    

    (Here, the sets are numbered from zero.)

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

Sidebar

Related Questions

Let's say we have interface window_creator that responsible for creation of windows. For simplicity
For the sake of simplicity, let's say that we have input strings with this
I have 2 arrays, for the sake of simplicity let's say the original one
For simplicity's sake, let's say I have two instances of HashMap<String, String> where they
Let's say I have an Access database table that has 2 columns: one is
For simplicity let's say I have two classes: a class called Car and a
let's say (for the sake of simplicity) that I have and event that when
Let's say I have a utility function that, for simplicity's sake (the real thing
For simplicity let's say I have code similar to this: def testMethod(String txt) {
For simplicity, let's say I want to do implement a function which takes two

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.