MARISSA MOLEJON | OCT2025-1 | Module-Complexity | Sprint 1 | Refactor/javascript complexity analysis - #2
Open
marissamolejon wants to merge 5 commits into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Analyzes the time and space complexity of all four Sprint 1 JavaScript
functions, documents the findings in JSDoc comments, and refactors the
functions where the complexity could genuinely be improved.
Closes #3
Changes made
calculateSumAndProductfindCommonItemsArray.includes()(O(m) per call) with aSet.has()lookup (O(1) per call) built from the second array up front.hasPairWithSumSetto check each number's complement (target - num).removeDuplicatesSetfor O(1) membership checks, preserving order of first occurrence.All four functions now have complete JSDoc blocks documenting Time
Complexity, Space Complexity, and Optimal Time Complexity, with brief
reasoning for each.
Testing
All existing tests pass unchanged for every function — no test files
were modified, since the goal was to make each function more efficient
without changing its observable behavior.
Ran locally:
node calculateSumAndProduct.test.js✓node findCommonItems.test.js✓node hasPairWithSum.test.js✓node removeDuplicates.test.mjs✓Learning points
factors) — this is different from nested loops, where one loop's work
is repeated inside every iteration of another, which multiplies rather
than adds.
Array.includes()andArray.indexOf()are O(n) per call because theyscan linearly; calling them inside another loop creates O(n²)-style
behavior even when it doesn't look like a "classic" nested loop.
Set-based lookup is a recurring,reusable pattern for turning O(n²) algorithms into O(n) — used it here
in
hasPairWithSumandremoveDuplicates, and a close variant(Set-from-the-second-array) in
findCommonItems.calculateSumAndProductwas already O(n); the refactor there was areadability/constant-factor improvement, not an algorithmic one, and
it's worth being explicit about that distinction rather than overselling
a small change.
Checklist