aboutsummaryrefslogtreecommitdiff
path: root/single_number.rs
blob: c724ebf207f0d41ec1f0302a4c6e5340fa32b977 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
struct Solution;

impl Solution {
    pub fn single_number(nums: Vec<i32>) -> i32 {
        let mut n = nums;
        n.sort();
        let mut tmp = vec![n[0]];
        for i in 1..n.len() {
            if tmp.len() == 0 || tmp[tmp.len() - 1] != n[i] {
                tmp.push(n[i]);
            } else {
                tmp.pop();
            }
        }
        tmp[0]
    }
}

fn main() {
    let nums = vec![2,2,1];
    print!("{}", Solution::single_number(nums));
}