Looking for some feedback on the Two Sum LeetCode problem. Looking for feedback on code style in general, use of var, variable naming and initialization, return placement, and any other feedback or optimizations I could make.
Problem
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
My solution
using System.Collections.Generic;
public class Solution {
public int[] TwoSum(int[] nums, int target) {
var numsDictionary = new Dictionary<int, int>();
int complement = 0;
for(int i=0; i < nums.Count(); i++)
{
complement = target - nums[i];
int index = 0;
if(numsDictionary.TryGetValue(complement, out index))
{
int[] twoSumSolution = {index, i};
return twoSumSolution;
}
if(!numsDictionary.ContainsKey(nums[i]))
{
numsDictionary.Add(nums[i], i);
}
}
return null;
}
}