Skip to main content
added 2 characters in body
Source Link
Heslacher
  • 51k
  • 5
  • 83
  • 177
  • Most C# developers will place opening braces on a new line. If you do this as well then you make it easier to read the code by others.

  • complement and index should use var type"type" as well

  • twoSumSolution isn't really needed and is only adding noise to the code.

  • I like to use if (bool == false) instead of if (!bool) because I can grasp it at first glance without wondering wether I see a exclamation sign or not.

  • Instead of using the Count() extension method I prefer using the Length property for arrays. Using Count() will envolve a soft cast to ICollection<T> and a null check which just isn't needed.

  • checking if complement > 0 will remove unneeded calls to TryGetValue(), but has implications on certain inputs like target = 0 and an array [-3, 7, 3].

Implementing the mentioned points will lead to

public int[] TwoSum(int[] nums, int target)
{
    var numsDictionary = new Dictionary<int, int>();

    var complement = 0;
    for (var i = 0; i < nums.Length; i++)
    {
        complement = target - nums[i];
        var index = 0;
        if (complement > 0 && numsDictionary.TryGetValue(complement, out index))
        {
            return new int[] { index, i };
        }

        if (numsDictionary.ContainsKey(nums[i]) == false)
        {
            numsDictionary.Add(nums[i], i);
        }
    }

    return null;
}
  • Most C# developers will place opening braces on a new line. If you do this as well then you make it easier to read the code by others.

  • complement and index should use var type as well

  • twoSumSolution isn't really needed and is only adding noise to the code.

  • I like to use if (bool == false) instead of if (!bool) because I can grasp it at first glance without wondering wether I see a exclamation sign or not.

  • Instead of using the Count() extension method I prefer using the Length property for arrays. Using Count() will envolve a soft cast to ICollection<T> and a null check which just isn't needed.

  • checking if complement > 0 will remove unneeded calls to TryGetValue(), but has implications on certain inputs like target = 0 and an array [-3, 7, 3].

Implementing the mentioned points will lead to

public int[] TwoSum(int[] nums, int target)
{
    var numsDictionary = new Dictionary<int, int>();

    var complement = 0;
    for (var i = 0; i < nums.Length; i++)
    {
        complement = target - nums[i];
        var index = 0;
        if (complement > 0 && numsDictionary.TryGetValue(complement, out index))
        {
            return new int[] { index, i };
        }

        if (numsDictionary.ContainsKey(nums[i]) == false)
        {
            numsDictionary.Add(nums[i], i);
        }
    }

    return null;
}
  • Most C# developers will place opening braces on a new line. If you do this as well then you make it easier to read the code by others.

  • complement and index should use var "type" as well

  • twoSumSolution isn't really needed and is only adding noise to the code.

  • I like to use if (bool == false) instead of if (!bool) because I can grasp it at first glance without wondering wether I see a exclamation sign or not.

  • Instead of using the Count() extension method I prefer using the Length property for arrays. Using Count() will envolve a soft cast to ICollection<T> and a null check which just isn't needed.

  • checking if complement > 0 will remove unneeded calls to TryGetValue(), but has implications on certain inputs like target = 0 and an array [-3, 7, 3].

Implementing the mentioned points will lead to

public int[] TwoSum(int[] nums, int target)
{
    var numsDictionary = new Dictionary<int, int>();

    var complement = 0;
    for (var i = 0; i < nums.Length; i++)
    {
        complement = target - nums[i];
        var index = 0;
        if (complement > 0 && numsDictionary.TryGetValue(complement, out index))
        {
            return new int[] { index, i };
        }

        if (numsDictionary.ContainsKey(nums[i]) == false)
        {
            numsDictionary.Add(nums[i], i);
        }
    }

    return null;
}
  • Most C# developers will place opening braces on a new line. If you do this as well then you make it easier to read the code by others.

  • complement and index should use var type as well

  • twoSumSolution isn't really needed and is only adding noise to the code.

  • I like to use if (bool == false) instead of if (!bool) because I can grasp it at first glance without wondering wether I see a exclamation sign or not.

  • Instead of using the Count() extension method I prefer using the Length property for arrays. Using Count() will envolve a soft cast to ICollection<T> and a null check which just isn't needed.

  • checking if complement > 0 will remove unneeded calls to TryGetValue(), but has implications on certain inputs like target = 0 and an array [-3, 7, 3].

Implementing the mentioned points will lead to

public int[] TwoSum(int[] nums, int target)
{
    var numsDictionary = new Dictionary<int, int>();

    var complement = 0;
    for (var i = 0; i < nums.Length; i++)
    {
        complement = target - nums[i];
        var index = 0;
        if (complement > 0 && numsDictionary.TryGetValue(complement, out index))
        {
            return new int[] { index, i };
        }

        if (numsDictionary.ContainsKey(nums[i]) == false)
        {
            numsDictionary.Add(nums[i], i);
        }
    }

    return null;
}
  • Most C# developers will place opening braces on a new line. If you do this as well then you make it easier to read the code by others.

  • complement and index should use var type as well

  • twoSumSolution isn't really needed and is only adding noise to the code.

  • I like to use if (bool == false) instead of if (!bool) because I can grasp it at first glance without wondering wether I see a exclamation sign or not.

  • Instead of using the Count() extension method I prefer using the Length property for arrays. Using Count() will envolve a soft cast to ICollection<T> and a null check which just isn't needed.

  • checking if complement > 0 will remove unneeded calls to TryGetValue().

Implementing the mentioned points will lead to

public int[] TwoSum(int[] nums, int target)
{
    var numsDictionary = new Dictionary<int, int>();

    var complement = 0;
    for (var i = 0; i < nums.Length; i++)
    {
        complement = target - nums[i];
        var index = 0;
        if (complement > 0 && numsDictionary.TryGetValue(complement, out index))
        {
            return new int[] { index, i };
        }

        if (numsDictionary.ContainsKey(nums[i]) == false)
        {
            numsDictionary.Add(nums[i], i);
        }
    }

    return null;
}
  • Most C# developers will place opening braces on a new line. If you do this as well then you make it easier to read the code by others.

  • complement and index should use var type as well

  • twoSumSolution isn't really needed and is only adding noise to the code.

  • I like to use if (bool == false) instead of if (!bool) because I can grasp it at first glance without wondering wether I see a exclamation sign or not.

  • Instead of using the Count() extension method I prefer using the Length property for arrays. Using Count() will envolve a soft cast to ICollection<T> and a null check which just isn't needed.

  • checking if complement > 0 will remove unneeded calls to TryGetValue(), but has implications on certain inputs like target = 0 and an array [-3, 7, 3].

Implementing the mentioned points will lead to

public int[] TwoSum(int[] nums, int target)
{
    var numsDictionary = new Dictionary<int, int>();

    var complement = 0;
    for (var i = 0; i < nums.Length; i++)
    {
        complement = target - nums[i];
        var index = 0;
        if (complement > 0 && numsDictionary.TryGetValue(complement, out index))
        {
            return new int[] { index, i };
        }

        if (numsDictionary.ContainsKey(nums[i]) == false)
        {
            numsDictionary.Add(nums[i], i);
        }
    }

    return null;
}
Source Link
Heslacher
  • 51k
  • 5
  • 83
  • 177

  • Most C# developers will place opening braces on a new line. If you do this as well then you make it easier to read the code by others.

  • complement and index should use var type as well

  • twoSumSolution isn't really needed and is only adding noise to the code.

  • I like to use if (bool == false) instead of if (!bool) because I can grasp it at first glance without wondering wether I see a exclamation sign or not.

  • Instead of using the Count() extension method I prefer using the Length property for arrays. Using Count() will envolve a soft cast to ICollection<T> and a null check which just isn't needed.

  • checking if complement > 0 will remove unneeded calls to TryGetValue().

Implementing the mentioned points will lead to

public int[] TwoSum(int[] nums, int target)
{
    var numsDictionary = new Dictionary<int, int>();

    var complement = 0;
    for (var i = 0; i < nums.Length; i++)
    {
        complement = target - nums[i];
        var index = 0;
        if (complement > 0 && numsDictionary.TryGetValue(complement, out index))
        {
            return new int[] { index, i };
        }

        if (numsDictionary.ContainsKey(nums[i]) == false)
        {
            numsDictionary.Add(nums[i], i);
        }
    }

    return null;
}