https://github.com/aliezzahn/remove_duplicates_sorted_array
This is a Rust implementation of the "Remove Duplicates from Sorted Array" problem
https://github.com/aliezzahn/remove_duplicates_sorted_array
Last synced: 5 months ago
JSON representation
This is a Rust implementation of the "Remove Duplicates from Sorted Array" problem
- Host: GitHub
- URL: https://github.com/aliezzahn/remove_duplicates_sorted_array
- Owner: aliezzahn
- License: mit
- Created: 2025-02-24T11:02:33.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2025-02-24T11:19:53.000Z (11 months ago)
- Last Synced: 2025-08-11T00:03:03.536Z (5 months ago)
- Language: Rust
- Size: 2.93 KB
- Stars: 10
- Watchers: 1
- Forks: 8
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Remove Duplicates from Sorted Array
This is a Rust implementation of the "Remove Duplicates from Sorted Array" problem
## Problem Description
Given an integer array `nums` sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same. Then return the number of unique elements in `nums`.
## Solution
The solution uses a two-pointer approach:
- One pointer (`i`) iterates through the array.
- Another pointer (`k`) places the next unique element.
- The algorithm runs in O(n) time and uses O(1) extra space.
## Usage
To use the solution, call the `remove_duplicates` function with a mutable reference to a sorted vector:
```rust
let mut nums = vec![1, 1, 2];
let k = Solution::remove_duplicates(&mut nums);
println!("Unique elements: {}", k); // Output: 2
println!("Modified array: {:?}", nums); // Output: [1, 2, 2]
```