Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

217. Contains Duplicate

Check if any value appears at least twice in nums.

HashSet approach:

#![allow(unused)]
fn main() {
pub fn contains_duplicate(nums: Vec<i32>) -> bool {
    let mut unique: HashSet<i32> = HashSet::with_capacity(nums.len());
    for num in nums {
        if !unique.insert(num) {
            return true;
        }
    }

    false
}
}