< Summary

Information
Class: LeetCode.Algorithms.InsertDeleteGetRandom.InsertDeleteGetRandomDictionaryList
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\InsertDeleteGetRandom\InsertDeleteGetRandomDictionaryList.cs
Line coverage
100%
Covered lines: 27
Uncovered lines: 0
Coverable lines: 27
Total lines: 76
Line coverage: 100%
Branch coverage
100%
Covered branches: 4
Total branches: 4
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
.ctor()100%11100%
Insert(...)100%22100%
Remove(...)100%22100%
GetRandom()100%11100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\InsertDeleteGetRandom\InsertDeleteGetRandomDictionaryList.cs

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2026 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.InsertDeleteGetRandom;
 13
 14/// <inheritdoc />
 15public sealed class InsertDeleteGetRandomDictionaryList : IInsertDeleteGetRandom
 16{
 117    private static readonly Random Random = new();
 118    private readonly List<int> _values = [];
 119    private readonly Dictionary<int, int> _valueToIndexDictionary = [];
 20
 21    /// <summary>
 22    ///     Time complexity - O(1)
 23    ///     Space complexity - O(1)
 24    /// </summary>
 25    /// <param name="value"></param>
 26    /// <returns></returns>
 27    public bool Insert(int value)
 428    {
 429        if (_valueToIndexDictionary.ContainsKey(value))
 130        {
 131            return false;
 32        }
 33
 334        _valueToIndexDictionary.Add(value, _values.Count);
 335        _values.Add(value);
 36
 337        return true;
 438    }
 39
 40    /// <summary>
 41    ///     Time complexity - O(1)
 42    ///     Space complexity - O(1)
 43    /// </summary>
 44    /// <param name="value"></param>
 45    /// <returns></returns>
 46    public bool Remove(int value)
 447    {
 448        if (!_valueToIndexDictionary.TryGetValue(value, out var index))
 249        {
 250            return false;
 51        }
 52
 253        var lastValueIndex = _values.Count - 1;
 254        var lastValue = _values[lastValueIndex];
 55
 256        _values[index] = lastValue;
 257        _values.RemoveAt(lastValueIndex);
 58
 259        _valueToIndexDictionary[lastValue] = index;
 260        _valueToIndexDictionary.Remove(value);
 61
 262        return true;
 463    }
 64
 65    /// <summary>
 66    ///     Time complexity - O(1)
 67    ///     Space complexity - O(1)
 68    /// </summary>
 69    /// <returns></returns>
 70    public int GetRandom()
 371    {
 372        var randomIndex = Random.Next(_values.Count);
 73
 374        return _values[randomIndex];
 375    }
 76}