---
# System prepended metadata

title: 1. Two Sum
tags: [C++, Rust, LeetCode-Easy]

---

# 1. Two Sum
Difficulty: Easy

## Solution - C++

```cpp=
class Solution {
 public:
  std::vector<int> twoSum(std::vector<int>& nums, int target) {
    const auto nums_size = nums.size();
    
    std::unordered_map<int, int> diff_map;
    for (size_t i = 0; i < nums_size; ++i) {
      const auto num = nums[i];
      const auto &it = diff_map.find(num);
      if (it != map.end()) {
        std::vector<int> answer = {it->second, static_cast<int>(i)};
        return answer;
      }
      diff_map[target - num] = static_cast<int>(i);
    }

    return std::vector<int>();
  }
};
```
## Result - C++


## Solution - Rust

```rust=
use std::collections::HashMap;

impl Solution {
    pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
        let nums_len: usize = nums.len();
        let mut diff_map:HashMap<i32, i32> = HashMap::new();
        for i in 0..nums_len {
            let num = nums[i];
            let x = diff_map.get(&num);
            if x != None {
                return vec![*x.unwrap(), i as i32];
            }
            
            diff_map.insert(target - num, i as i32);
        }
        
        return vec![];
    }
}

```

## Result - Rust

Success
Details 
Runtime: 0 ms, faster than 100.00% of Rust online submissions for Two Sum.
Memory Usage: 2.5 MB, less than 20.97% of Rust online submissions for Two Sum.

###### tags: `LeetCode-Easy` `Rust` `C++`
