< Summary

Information
Class: LeetCode.Algorithms.RemoveDuplicatesFromSortedArray2.RemoveDuplicatesFromSortedArray2Dictionary
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\RemoveDuplicatesFromSortedArray2\RemoveDuplicatesFromSortedArray2Dictionary.cs
Line coverage
100%
Covered lines: 25
Uncovered lines: 0
Coverable lines: 25
Total lines: 58
Line coverage: 100%
Branch coverage
100%
Covered branches: 8
Total branches: 8
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
RemoveDuplicates(...)100%88100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\RemoveDuplicatesFromSortedArray2\RemoveDuplicatesFromSortedArray2Dictionary.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.RemoveDuplicatesFromSortedArray2;
 13
 14/// <inheritdoc />
 15public class RemoveDuplicatesFromSortedArray2Dictionary : IRemoveDuplicatesFromSortedArray2
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n^2)
 19    ///     Space complexity - O(n)
 20    /// </summary>
 21    /// <param name="nums"></param>
 22    /// <returns></returns>
 23    public int RemoveDuplicates(int[] nums)
 224    {
 225        var numsDictionary = new Dictionary<int, int>();
 26
 227        var left = 0;
 228        var right = nums.Length;
 29
 1730        while (left < right)
 1531        {
 1532            if (numsDictionary.TryAdd(nums[left], 1))
 733            {
 734                left++;
 35
 736                continue;
 37            }
 38
 839            if (numsDictionary[nums[left]] < 2)
 540            {
 541                numsDictionary[nums[left]]++;
 42
 543                left++;
 544            }
 45            else
 346            {
 2647                for (var j = left; j < right - 1; j++)
 1048                {
 1049                    (nums[j], nums[j + 1]) = (nums[j + 1], nums[j]);
 1050                }
 51
 352                right--;
 353            }
 854        }
 55
 256        return right;
 257    }
 58}