From 76ffc98ed4ab8e537437efc5e79caeaa5b59bc6b Mon Sep 17 00:00:00 2001 From: Bauke Date: Mon, 11 Apr 2022 13:23:20 +0200 Subject: [PATCH] Solve contains-duplicate. --- source/contains_duplicate/mod.rs | 8 ++++++++ source/lib.rs | 1 + tests/contains_duplicate.rs | 9 +++++++++ 3 files changed, 18 insertions(+) create mode 100644 source/contains_duplicate/mod.rs create mode 100644 tests/contains_duplicate.rs diff --git a/source/contains_duplicate/mod.rs b/source/contains_duplicate/mod.rs new file mode 100644 index 0000000..97883e4 --- /dev/null +++ b/source/contains_duplicate/mod.rs @@ -0,0 +1,8 @@ +pub fn contains_duplicate(numbers: Vec) -> bool { + // Uncomment FromIterator in Leetcode since they're using Rust Edition 2018. + // Edition 2021 has this trait in its prelude. + + // use std::iter::FromIterator; + std::collections::HashSet::<_>::from_iter(numbers.iter()).len() + != numbers.len() +} diff --git a/source/lib.rs b/source/lib.rs index efcc123..9c29e04 100644 --- a/source/lib.rs +++ b/source/lib.rs @@ -1,4 +1,5 @@ pub mod add_binary; +pub mod contains_duplicate; pub mod excel_sheet_column_number; pub mod excel_sheet_column_title; pub mod implement_strstr; diff --git a/tests/contains_duplicate.rs b/tests/contains_duplicate.rs new file mode 100644 index 0000000..b12d687 --- /dev/null +++ b/tests/contains_duplicate.rs @@ -0,0 +1,9 @@ +use leetcode::contains_duplicate::contains_duplicate; + +use test_case::test_case; + +#[test_case(&[1, 2, 3, 1], true; "example 1")] +#[test_case(&[1, 2, 3, 4], false; "example 2")] +fn test_contains_duplicate(input: &[i32], expected: bool) { + assert_eq!(contains_duplicate(input.to_vec()), expected); +}