Binary Search in Python: Find First Entry in List with Duplicates

preview_player
Показать описание
In this video, we will be writing a function that takes an array of sorted integers and a key and returns the index of the first occurrence of that key from the array.

For example, for the array:
[-14, -10, 2, 108, 108, 243, 285, 285, 285, 401]

with target = 108, the algorithm would return 3, as the first occurrence of 108 in the above array is located at index 3.

This video is one part of the Binary Search playlist on my channel. For more videos on binary search and how to apply it to various problems, check out the other videos:

The software written in this video is available at:

Do you like the development environment I'm using in this video? It's a customized version of vim that's enhanced for Python development. If you want to see how I set up my vim, I have a series on this here:

If you've found this video helpful and want to stay up-to-date with the latest videos posted on this channel, please subscribe:
Рекомендации по теме
Комментарии
Автор

OMG, this is such an underrated channel for DS and Algorithms.

Great work

Ovicron
Автор

I wonder if the explanation could be any clearer. Thanks man, awesome work ;)

evgenyocean
Автор

Hey buddy, great work your doing here. Just wanted to suggest an improvement to your algorithm for when you find an occurrence
else:
while A[mid] == A[mid - 1]:
mid = mid-1
Return mid

monke
Автор

Hi! I have a question, what if we want all the positions of the target 108? or 285?

takwa
Автор

How would you modify this to find the last occurrence?

TooManyPBJs