From 1dbae4a9593a3901d8e0685231c32fa614193c16 Mon Sep 17 00:00:00 2001 From: Vivek Kumar Rathour Date: Mon, 24 Nov 2025 14:10:35 +0530 Subject: [PATCH] Add test case for binary_search with duplicates This adds a doctest example showing that binary_search correctly handles lists with duplicate elements. Fixes #13886 The binary search algorithm correctly returns one valid index of the target element, even when there are duplicates in the sorted list. --- searches/binary_search.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/searches/binary_search.py b/searches/binary_search.py index 2e66b672d5b4..2eac049e3ef7 100644 --- a/searches/binary_search.py +++ b/searches/binary_search.py @@ -197,6 +197,8 @@ def binary_search(sorted_collection: list[int], item: int) -> int: 1 >>> binary_search([0, 5, 7, 10, 15], 6) -1 + >>> binary_search([1, 2, 2, 2, 3], 2) # Returns one valid index of 2 + 2 """ if list(sorted_collection) != sorted(sorted_collection): raise ValueError("sorted_collection must be sorted in ascending order")