Skip to content

Fix incorrect output in Binary Search when duplicates exist(Fixes #13840)#14322

Closed
tusharynayaka wants to merge 1 commit intoTheAlgorithms:masterfrom
tusharynayaka:fix-binary-search-duplicates
Closed

Fix incorrect output in Binary Search when duplicates exist(Fixes #13840)#14322
tusharynayaka wants to merge 1 commit intoTheAlgorithms:masterfrom
tusharynayaka:fix-binary-search-duplicates

Conversation

@tusharynayaka
Copy link

Description of Change
Updated the binary search algorithm to return the first occurrence of an item when duplicates exist in the collection, rather than returning any random occurrence.
Added a check to see if the element immediately to the left of the found midpoint is also the target item.
If it is, the search continues to the left side to find the true first index.
Updated doctests to verify this behavior.

Checklist:
[x] I have read CONTRIBUTING.md.
[x] This pull request is all my own work -- I have not plagiarized.
[x] I know that pull requests will not be merged if they fail the automated tests.
[x] This PR only changes one algorithm file.
[x] All filenames are in all lowercase characters with no spaces or dashes.
[x] All functions and variable names follow Python naming conventions.
[x] All function parameters and return values are annotated with Python type hints.
[x] All functions have doctests that pass the automated testing.
[x] If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword: Fixes #13840

@algorithms-keeper
Copy link

Closing this pull request as invalid

@tusharynayaka, this pull request is being closed as the files submitted contains an invalid extension. This repository only accepts Python algorithms. Please read the Contributing guidelines first.

Invalid files in this pull request: uv.lock

@algorithms-keeper algorithms-keeper bot closed this Mar 2, 2026
@algorithms-keeper algorithms-keeper bot added the awaiting reviews This PR is ready to be reviewed label Mar 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed invalid

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect output for Binary Search when list contains duplicates

1 participant