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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:59:14+00:00 2026-06-17T15:59:14+00:00

Consider the following example: package main import ( fmt sort ) func main() {

  • 0

Consider the following example:

package main

import (
    "fmt"
    "sort"
)

func main() {
    var n int
    var a sort.IntSlice
    a = append(a, 23)   
    a = append(a, 3)
    a = append(a, 10)
    sort.Sort(a)    
    fmt.Println(a)
    n = sort.SearchInts(a, 1)
    fmt.Println(n)
    n = sort.SearchInts(a, 3)
    fmt.Println(n)
}

http://play.golang.org/p/wo4r43Zghv

And the result is:

[3 10 23]
0
0

How am I supposed to know if the number is present in the slice or not, when both the first element and the nonexistent element return 0 as index?

Update
Note that the index can also be greater than the length of the slice so the proper way to find if an element is present in the slice is:

num := 1
n = sort.SearchInts(a, num) 
if n < len(a) && a[n] == num {
  // found
}
  • 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-17T15:59:15+00:00Added an answer on June 17, 2026 at 3:59 pm

    Check whether the number you were looking for actually lives at the returned index. The binary search routines in sort are supposed to find the index where a value can be inserted if it’s not already present.

    E.g. a search for 100 in the slice in your example will return 3 because the value must be appended to the slice.

    the call Search(len(data), func(i int) bool { return data[i] >= 23 }) returns the smallest index i such that data[i] >= 23. If the caller wants to find whether 23 is in the slice, it must test data[i] == 23 separately.

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

Sidebar

Related Questions

Consider following example. #include <iostream> #include <algorithm> #include <vector> #include <boost/bind.hpp> void func(int e,
Consider following example: #include <stdlib.h> #include <stdio.h> #include <errno.h> #include <hiredis/hiredis.h> int main(int argc,
Consider the following example program: next :: Int -> Int next i | 0
Consider the following example: int size = 10, *kk = new int[size]; for (int
Is there anyway I can import modules to a package level? for example, consider
Consider the following Hibernate mappings: <hibernate-mapping package=org.example> <class name=Customer table=CUSTOMER> <id name=customerId column=customer_id/> <bag
Consider following example. #include <iostream> #include <boost/optional.hpp> template < typename A > int boo(
Consider the following example: struct MyStruct { int a; int b; }; I can
Consider the following example. String str = new String(); str = "Hello"; System.out.println(str); //Prints
Consider following example code: #include <iostream> #include <inttypes.h> using namespace std; int f(uint32_t i)

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.