217. Contains Duplicate

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.

Example 1:

Input: [1,2,3,1]
Output: true

Example 2:

Input: [1,2,3,4]
Output: false

Example 3:

Input: [1,1,1,3,3,4,3,2,4,2]
Output: true

Rust Solution

struct Solution;

use std::collections::HashSet;

impl Solution {
    fn contains_duplicate(nums: Vec<i32>) -> bool {
        let mut hs: HashSet<i32> = HashSet::new();
        for n in nums {
            if hs.contains(&n) {
                return true;
            } else {
                hs.insert(n);
            }
        }
        false
    }
}

#[test]
fn test() {
    let nums = vec![1, 2, 3, 1];
    assert_eq!(Solution::contains_duplicate(nums), true);
    let nums = vec![1, 2, 3, 4];
    assert_eq!(Solution::contains_duplicate(nums), false);
    let nums = vec![1, 1, 1, 3, 3, 4, 3, 2, 4, 2];
    assert_eq!(Solution::contains_duplicate(nums), true);
}

Having problems with this solution? Click here to submit an issue on github.