Skip to content

feat(data structures, arrays): contains duplicates#114

Merged
BrianLusina merged 3 commits intomainfrom
feat/contains-duplicates
Dec 2, 2025
Merged

feat(data structures, arrays): contains duplicates#114
BrianLusina merged 3 commits intomainfrom
feat/contains-duplicates

Conversation

@BrianLusina
Copy link
Copy Markdown
Owner

@BrianLusina BrianLusina commented Dec 2, 2025

Describe your change:

Adds a new algorithm utilising Sliding window technique to find duplicates in an array which are at most k distance apart.

  • [s] Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

Summary by CodeRabbit

  • New Features

    • Added an enhanced nearby-duplicate detection capability (sliding-window approach).
  • Documentation

    • Expanded algorithm docs with constraints, examples, step-by-step solution and complexity analysis.
  • Chores

    • Updated navigation/menu to surface new sections and tests.
    • Reorganized tests: added consolidated unit tests for nearby-duplicate cases and removed older redundant test entries.

✏️ Tip: You can customize this high-level summary in your review settings.

@BrianLusina BrianLusina self-assigned this Dec 2, 2025
@BrianLusina BrianLusina added enhancement Algorithm Algorithm Problem Datastructures Datastructures Documentation Documentation Updates Array Array data structure Sliding Window labels Dec 2, 2025
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Dec 2, 2025

Caution

Review failed

The pull request is closed.

Walkthrough

Adds a sliding-window implementation contains_nearby_duplicates_2, expands documentation for "Contains Duplicates II", moves tests into the package source, and updates navigation entries in DIRECTORY.md.

Changes

Cohort / File(s) Summary
Navigation / Docs
DIRECTORY.md, datastructures/arrays/contains_duplicates/README.md
Added navigation entries (Frog Position, Binary Tree) and expanded README with a full "Contains Duplicates II" section: constraints, updated examples, sliding-window solution steps, complexity notes, and illustrations.
Implementation
datastructures/arrays/contains_duplicates/__init__.py
Added contains_nearby_duplicates_2(nums: List[int], k: int) -> bool using a set-based sliding window that evicts oldest elements when window exceeds k; existing function preserved.
Tests (moved)
datastructures/arrays/contains_duplicates/test_contains_nearby_duplicate.py
Added unit tests (unittest) covering contains_nearby_duplicate and contains_nearby_duplicates_2 with multiple parameterized scenarios.
Tests (removed)
tests/datastructures/arrays/test_contains_nearby_duplicate.py
Removed legacy test file from the top-level tests/ tree.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Review sliding-window eviction logic and boundary conditions (k = 0, empty list, large k).
  • Confirm tests cover both functions and edge cases (duplicates at distance exactly k, no duplicates).
  • Verify README examples match implemented behavior and that moving tests didn't break test discovery or CI paths.

Poem

🐰 I nibble code beneath the moon,
A sliding window hums a tune.
Duplicates hop, then I find,
Tests hop home and docs aligned.
Cheers, small changes — carrot-lined! 🥕

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title 'feat(data structures, arrays): contains duplicates' clearly identifies the main change as adding contains duplicates functionality to the arrays data structure module.
Description check ✅ Passed The PR description follows the template structure, clearly describes the change (adding a sliding window algorithm), and marks most checklist items as completed.

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 86c453f and 946769d.

📒 Files selected for processing (1)
  • datastructures/arrays/contains_duplicates/README.md (2 hunks)

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
datastructures/arrays/contains_duplicates/__init__.py (1)

29-58: Consider using enumerate for more Pythonic iteration.

The implementation is correct, but the loop could be more idiomatic.

Apply this diff to make the code more Pythonic:

-    for idx in range(len(nums)):
+    for idx, num in enumerate(nums):
         # If we have seen this number before
-        if nums[idx] in seen:
+        if num in seen:
             # Return True
             return True
         # Add the current number to the set of seen numbers
-        seen.add(nums[idx])
+        seen.add(num)
 
         # If we have seen more than k numbers
         if len(seen) > k:
             # Remove the number that is k steps behind the current number
             seen.remove(nums[idx - k])
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5493cec and 86c453f.

⛔ Files ignored due to path filters (11)
  • datastructures/arrays/contains_duplicates/images/solutions/contains_duplicates_ii_solution_1.png is excluded by !**/*.png
  • datastructures/arrays/contains_duplicates/images/solutions/contains_duplicates_ii_solution_10.png is excluded by !**/*.png
  • datastructures/arrays/contains_duplicates/images/solutions/contains_duplicates_ii_solution_11.png is excluded by !**/*.png
  • datastructures/arrays/contains_duplicates/images/solutions/contains_duplicates_ii_solution_2.png is excluded by !**/*.png
  • datastructures/arrays/contains_duplicates/images/solutions/contains_duplicates_ii_solution_3.png is excluded by !**/*.png
  • datastructures/arrays/contains_duplicates/images/solutions/contains_duplicates_ii_solution_4.png is excluded by !**/*.png
  • datastructures/arrays/contains_duplicates/images/solutions/contains_duplicates_ii_solution_5.png is excluded by !**/*.png
  • datastructures/arrays/contains_duplicates/images/solutions/contains_duplicates_ii_solution_6.png is excluded by !**/*.png
  • datastructures/arrays/contains_duplicates/images/solutions/contains_duplicates_ii_solution_7.png is excluded by !**/*.png
  • datastructures/arrays/contains_duplicates/images/solutions/contains_duplicates_ii_solution_8.png is excluded by !**/*.png
  • datastructures/arrays/contains_duplicates/images/solutions/contains_duplicates_ii_solution_9.png is excluded by !**/*.png
📒 Files selected for processing (5)
  • DIRECTORY.md (3 hunks)
  • datastructures/arrays/contains_duplicates/README.md (2 hunks)
  • datastructures/arrays/contains_duplicates/__init__.py (1 hunks)
  • datastructures/arrays/contains_duplicates/test_contains_nearby_duplicate.py (1 hunks)
  • tests/datastructures/arrays/test_contains_nearby_duplicate.py (0 hunks)
💤 Files with no reviewable changes (1)
  • tests/datastructures/arrays/test_contains_nearby_duplicate.py
🧰 Additional context used
🧬 Code graph analysis (1)
datastructures/arrays/contains_duplicates/test_contains_nearby_duplicate.py (1)
datastructures/arrays/contains_duplicates/__init__.py (2)
  • contains_nearby_duplicate (5-26)
  • contains_nearby_duplicates_2 (29-58)
🪛 LanguageTool
datastructures/arrays/contains_duplicates/README.md

[grammar] ~26-~26: Ensure spelling is correct
Context: ...ums[i] == nums[j] and abs(i - j) <= k. Constrains: - 1 <= nums.length <= 10^3 - -10^3 <= nums...

(QB_NEW_EN_ORTHOGRAPHY_ERROR_IDS_1)

🪛 markdownlint-cli2 (0.18.1)
DIRECTORY.md

83-83: Unordered list indentation
Expected: 2; Actual: 4

(MD007, ul-indent)


84-84: Unordered list indentation
Expected: 4; Actual: 6

(MD007, ul-indent)


197-197: Unordered list indentation
Expected: 2; Actual: 4

(MD007, ul-indent)


198-198: Unordered list indentation
Expected: 4; Actual: 6

(MD007, ul-indent)


199-199: Unordered list indentation
Expected: 4; Actual: 6

(MD007, ul-indent)


296-296: Unordered list indentation
Expected: 6; Actual: 8

(MD007, ul-indent)


297-297: Unordered list indentation
Expected: 6; Actual: 8

(MD007, ul-indent)


298-298: Unordered list indentation
Expected: 6; Actual: 8

(MD007, ul-indent)


299-299: Unordered list indentation
Expected: 6; Actual: 8

(MD007, ul-indent)

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
@BrianLusina BrianLusina merged commit 6da8745 into main Dec 2, 2025
5 of 8 checks passed
@BrianLusina BrianLusina deleted the feat/contains-duplicates branch December 2, 2025 06:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Algorithm Algorithm Problem Array Array data structure Datastructures Datastructures Documentation Documentation Updates enhancement Sliding Window

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant