From 48d437c878b1eeabc0b1ba2f94c37354ba729abf Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Mon, 25 Mar 2024 12:40:33 +0100 Subject: [PATCH] =?UTF-8?q?rs:=20add=20=C2=AB442.=20Find=20All=20Duplicate?= =?UTF-8?q?s=20in=20an=20Array=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- rs/find-all-duplicates-in-an-array.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 rs/find-all-duplicates-in-an-array.rs diff --git a/rs/find-all-duplicates-in-an-array.rs b/rs/find-all-duplicates-in-an-array.rs new file mode 100644 index 0000000..fc2a2d6 --- /dev/null +++ b/rs/find-all-duplicates-in-an-array.rs @@ -0,0 +1,17 @@ +impl Solution { + pub fn find_duplicates(mut nums: Vec) -> Vec { + let mut duplicates = vec![]; + + for i in 0..nums.len() { + let index = nums[i].abs() - 1; + + if nums[index as usize] < 0 { + duplicates.push(index + 1); + } else { + nums[index as usize] *= -1; + } + } + + duplicates + } +}