Sales by Match

  • + 0 comments

    Rust best solution

    If you’re looking for solutions to the 3-month preparation kit in either Python or Rust, you can find them below: my solutions

    fn sales_by_match(n: i32, ar: &[i32]) -> i32 {
        //Time complexity: O(n)
        //Space complexity (ignoring input): O(n)
        let mut socks_hash = std::collections::HashMap::new();
        for sock in ar {
            match socks_hash.get(sock) {
                Some(v) => socks_hash.insert(sock, v + 1),
                None => socks_hash.insert(sock, 1),
            };
        }
        let mut total_pairs = 0;
        for values in socks_hash.values() {
            total_pairs += values / 2;
        }
        total_pairs
    }