Binary Search

Find the index of an element in a sorted array using binary search with O(log n) time complexity.

Code

Algorithms
echo "$arr" | jq --argjson t "$target" 'to_entries | map(select(.value == $t)) | if length > 0 then .[0].key else -1 end'

Parameters

Sorted array (JSON)

Element to find

Server

More Bash Snippets