< Summary

Information
Class: LeetCode.Algorithms.TwoSum.TwoSumDictionary
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\TwoSum\TwoSumDictionary.cs
Line coverage
93%
Covered lines: 14
Uncovered lines: 1
Coverable lines: 15
Total lines: 45
Line coverage: 93.3%
Branch coverage
83%
Covered branches: 5
Total branches: 6
Branch coverage: 83.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
TwoSum(...)83.33%6693.33%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\TwoSum\TwoSumDictionary.cs

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv).
 3// All Rights Reserved.
 4// --------------------------------------------------------------------------------
 5// This software is the confidential and proprietary information of Eugene Eremeev
 6// (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not
 7// disclose such Confidential Information and shall use it only in accordance with
 8// the terms of the license agreement you entered into with Eugene Eremeev (also
 9// known as Yevhenii Yeriemeieiv).
 10// --------------------------------------------------------------------------------
 11
 12namespace LeetCode.Algorithms.TwoSum;
 13
 14/// <inheritdoc />
 15public class TwoSumDictionary : ITwoSum
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n)
 19    ///     Space complexity - O(n)
 20    /// </summary>
 21    /// <param name="nums"></param>
 22    /// <param name="target"></param>
 23    /// <returns></returns>
 24    public int[] TwoSum(int[] nums, int target)
 425    {
 426        var dictionary = new Dictionary<int, int>();
 27
 2028        for (var i = 0; i < nums.Length; i++)
 1029        {
 1030            var complement = target - nums[i];
 31
 1032            if (dictionary.TryGetValue(complement, out var value))
 433            {
 434                return [value, i];
 35            }
 36
 637            if (!dictionary.ContainsKey(nums[i]))
 638            {
 639                dictionary[nums[i]] = i;
 640            }
 641        }
 42
 043        return [];
 444    }
 45}