From c55485c968b88a13e0422e97178ce7c72d6d35ba Mon Sep 17 00:00:00 2001 From: Jonir Rings Date: Thu, 21 Mar 2019 18:17:50 +0800 Subject: [PATCH] Update n0026_remove_duplicates_from_sorted_array.rs --- src/n0026_remove_duplicates_from_sorted_array.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/n0026_remove_duplicates_from_sorted_array.rs b/src/n0026_remove_duplicates_from_sorted_array.rs index 50890a92..c5794fac 100644 --- a/src/n0026_remove_duplicates_from_sorted_array.rs +++ b/src/n0026_remove_duplicates_from_sorted_array.rs @@ -49,8 +49,18 @@ pub struct Solution {} impl Solution { pub fn remove_duplicates(nums: &mut Vec) -> i32 { - nums.dedup(); - nums.len() as i32 + let len = nums.len(); + if len <= 1 { + return len as i32; + } + let mut slow = 0usize; + for fast in 1..len { + if nums[slow] != nums[fast] { + slow += 1; + nums[slow] = nums[fast]; + } + } + (slow + 1) as i32 } }