## Understanding the array.binary_search function in Pine Script

Arrays are an essential part of programming, and they allow us to store and manipulate a large set of data. In Pine Script, the ** array.binary_search** function is a useful tool to find the index of an element within an array. In this tutorial, we will learn how to use the

**function in Pine Script.**

`array.binary_search`

## What is array.binary_search Function in Pine Script?

The ** array.binary_search** function is a built-in function in Pine Script that is used to find the index of a specified element within an array. It uses the binary search algorithm to perform the search, which makes it an efficient and fast way to find the index of an element within a sorted array.

Here’s the syntax of the ** array.binary_search** function in Pine Script:

array.binary_search(arr, value)

Where ** arr** is the array in which we want to find the index of the specified

**.**

`value`

### Example 1: Finding the Index of an Element in an Array

Let’s consider the following example:

//@version=5 indicator("Binary Search Example 1", overlay=true) // Define an array arr = array.from(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) // Find the index of value 5 in the array index = array.binary_search(arr, 5) label l = na // Output the index value if barstate.islast l := label.new(bar_index , high , text = "Index of Number 5 = " + str.tostring(index ) , size =size.large , textcolor = color.white) label.delete(l[1])

In this example, we have defined an array arrwith ten elements. We want to find the index of the value 5 in this array. To do this, we call the array.binary_searchfunction and pass the array arrand the value 5as parameters. The function returns the index of the value 5, which we store in the indexvariable. Finally, we plot the value of indexusing the plotfunction.

## Explanation of the Example Code

Now let’s take a closer look at each line of code in the two examples we just went over.

### Example

//@version=5 indicator("Binary Search Example 1", overlay=true)

In this code block, we specify the Pine Script version and set the indicator’s name to “Binary Search Example 1”. The overlay=trueargument is used to plot the indicator on top of the price chart.

arr = array.from(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

In this code block, we define an array arrwith ten elements, using the array.from function.

index = array.binary_search(arr, 5)

In this line of code, we call the array.binary_search function and pass the arr array and the value 5 as parameters. The function returns the index of the value 5 in the arr array, which we store in the index variable.

label l = na // Output the index value if barstate.islast l := label.new(bar_index , high , text = "Index of Number 5 = " + str.tostring(index ) , size =size.large , textcolor = color.white) label.delete(l[1])

In this line of code, we plot the value of the **`index`

**variable using the ** label.new** function.

## Conclusion

In this tutorial, we have learned how to use the ** array.binary_search** function in Pine Script to find the index of an element within an array. We have also seen an example of how to use this function to find the range of indices of a specified element within a sorted array. By using the

**function, we can perform these tasks more efficiently and quickly than by using other search algorithms.**

`array.binary_search`

As a reminder, here are the two key points to remember about ** array.binary_search**:

- It is a built-in function in Pine Script that is used to find the index of a specified element within an array.
- It uses the binary search algorithm to perform the search, which makes it an efficient and fast way to find the index of an element within a sorted array.

I hope this tutorial has helped you understand how to use the ** array.binary_search** function in Pine Script. If you have any questions or comments, feel free to leave them below!