feat: implement and test remove_duplicates from list #222
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.
Overview
This PR implements a solution to the "Remove Duplicates from Sorted Array" problem using Cairo. The implementation provides a function that processes a sorted array and removes duplicate elements while maintaining the original order of unique elements.
Implementation Details
Approach
The implementation follows these key steps:
This approach ensures O(n) time complexity, where n is the size of the input array.
Cairo-specific Considerations
Several aspects of Cairo required special attention:
Differences Between In-place Modification in Traditional Languages vs. Cairo
The concept of "in-place" modification differs significantly between traditional languages and Cairo:
Traditional Languages (e.g., C++, Java, Python)
In traditional languages, in-place modifications typically:
In Cairo:
The approach in Cairo is functionally equivalent to in-place modification from the user's perspective but requires additional memory and operations to achieve.
Test Cases
The implementation includes comprehensive test cases covering:
Empty Array: Testing the edge case of an empty input array
No Duplicates: Testing arrays with all unique elements
Example Cases: Testing the examples provided in the requirements
All Duplicates: Testing an array where all elements are duplicates
Optimized Implementation: Parallel tests for the gas-optimized implementation
Each test verifies both the return value (number of unique elements) and the content of the modified array to ensure correctness.
Gas Efficiency Analysis
During testing, observed a significant difference in gas consumption between the standard and optimized implementations:
test_remove_duplicates_example2: ~131530 L2 gas
test_remove_duplicates_optimized_example2: ~109390 L2 gas
This represents approximately a 20% reduction in gas usage for the optimized implementation when processing the same input. The gas savings primarily come from:
These gas savings could be significant for applications processing large arrays or making frequent calls to this function. While both implementations provide the same functionality and correctness guarantees, the optimized version should be preferred in gas-sensitive contexts.
Screenshots
Summary
This implementation successfully solves the "Remove Duplicates from Sorted Array" problem in Cairo while accounting for the language's functional and immutable paradigm. Although Cairo's constraints required a different approach than traditional languages, the solution maintains the expected behavior and performance characteristics.
The key achievement is simulating in-place modification in a language that doesn't natively support such operations, while maintaining clarity and gas efficiency.
Closes #214