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

  • Home
  • SEARCH
  • 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 7799307
In Process

The Archive Base Latest Questions

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

Background I currently have an array like this: [1,1,2,3,4,5,5,5,6,7,8,8,8,8,9,10] I have been using a

  • 0

Background

I currently have an array like this:

[1,1,2,3,4,5,5,5,6,7,8,8,8,8,9,10]

I have been using a great JS Binary Search formula from this website:

searchArray = function(needle, haystack, case_insensitive) {
    if (typeof(haystack) === 'undefined' || !haystack.length) return -1;
    
    var high = haystack.length - 1;
    var low = 0;
    case_insensitive = (typeof(case_insensitive) === 'undefined' || case_insensitive) ? true:false;
    needle = (case_insensitive) ? needle.toLowerCase():needle;
    
    while (low <= high) {
        mid = parseInt((low + high) / 2)
        element = (case_insensitive) ? haystack[mid].toLowerCase():haystack[mid];
        if (element > needle) {
            high = mid - 1;
        } else if (element < needle) {
            low = mid + 1;
        } else {
            return mid;
        }
    }
    
    return -1;
};

This works fine for returning a single value.

Question

How do I return a range rather than a single value? For example, how would I return all values of 8 from the array, but STILL use the binary search (I do not want to loop through everything!!).

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-02T00:16:01+00:00Added an answer on June 2, 2026 at 12:16 am

    Something like this? http://jsfiddle.net/DCLey/3/

    var arr = ['1','1','2','3','4','5','5','5','6','7','8','8','8','8','9','10'];
            //  0   1   2   3   4   5   6   7   8   9   10  11  12  13  14  15            
    var searchArray = function(needle, haystack, case_insensitive) {
        if (typeof(haystack) === 'undefined' || !haystack.length) return -1;
    
        var high = haystack.length - 1;
        var low = 0;
        var vals = []; 
        var bUp = true; 
        var bDown = true;
        var i = 1; 
        case_insensitive = (typeof(case_insensitive) === 'undefined' || case_insensitive) ? true:false;
        needle = (case_insensitive) ? needle.toLowerCase():needle;
    
        while (low <= high) {
            mid = parseInt((low + high) / 2)
            element = (case_insensitive) ? haystack[mid].toLowerCase():haystack[mid];
            if (element > needle) {
                high = mid - 1;
            } else if (element < needle) {
                low = mid + 1;
            } else {
                vals.push(mid); 
                while(bUp || bDown){
                    if(bUp && haystack[mid] === haystack[mid + i]){
                        vals.push(mid + i); 
                    }else{
                        bUp = false;   
                    }
                    if(bDown && haystack[mid] === haystack[mid - i]){
                        vals.push(mid - i); 
                    }else{
                        bDown = false;   
                    }
                    i++;
                }
                return vals; 
            }
        }
    
        return -1;
    };
    
    alert(searchArray('8', arr, true)); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a website that I'm making that needs a background resizer. Currently, I
I have an array like this: var gridArray = [ 1,2,0,1,2,3,2,1,2,3,4,4,3,2,2,2,0 ] And I
I have an Nx4 array of vertices that I'd like to plot using glVertexArray
I currently have a complete cover background image. Here is the code: #back{ /*
Background Currently, I am working on a Rails application. I have different products that
Background Info: File Replication is Lame Currently, we have a massive, high-traffic ASP.NET web
I have this query which works correctly in MySQL. More background on it here
Background : I am currently using custom controls within my C# project (basic controls
OK so for background I've only been using Java for a little more than
So i've been working on my website in which i'd like to add a

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.