Given an array of integers, find if the array contains any duplicates.
Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.
Input: [1,2,3,1] Output: true
Input: [1,2,3,4] Output: false
Input: [1,1,1,3,3,4,3,2,4,2] Output: true
use std::collections::HashSet;
impl Solution {
pub fn contains_duplicate(nums: Vec<i32>) -> bool {
nums.len() > nums.iter().collect::<HashSet<_>>().len()
}
}
impl Solution {
pub fn contains_duplicate(nums: Vec<i32>) -> bool {
let len = nums.len();
for i in 0..len {
for j in (i + 1)..len {
if nums[i] == nums[j] {
return true;
}
}
}
false
}
}
use std::collections::HashSet;
impl Solution {
pub fn contains_duplicate(nums: Vec<i32>) -> bool {
let mut set = HashSet::new();
for n in nums {
if set.contains(&n) {
return true;
} else {
set.insert(n);
}
}
false
}
}
impl Solution {
pub fn contains_duplicate(nums: Vec<i32>) -> bool {
let mut nums = nums;
nums.sort_unstable();
for i in 1..nums.len() {
if nums[i - 1] == nums[i] {
return true;
}
}
false
}
}