1
Fork 0

Solve contains-duplicate.

This commit is contained in:
Bauke 2022-04-11 13:23:20 +02:00
parent 184e456e1b
commit 76ffc98ed4
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
3 changed files with 18 additions and 0 deletions

View File

@ -0,0 +1,8 @@
pub fn contains_duplicate(numbers: Vec<i32>) -> 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()
}

View File

@ -1,4 +1,5 @@
pub mod add_binary; pub mod add_binary;
pub mod contains_duplicate;
pub mod excel_sheet_column_number; pub mod excel_sheet_column_number;
pub mod excel_sheet_column_title; pub mod excel_sheet_column_title;
pub mod implement_strstr; pub mod implement_strstr;

View File

@ -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);
}