using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.IO; using System.Xml; using System.Text.RegularExpressions; public interface ICodeRush { int? FindLeastFrequentNumber(IList list); } using System.Collections.Generic; using System.Linq; public class Solution : ICodeRush { private readonly Dictionary _fDict = new Dictionary(); private class CountAndKey : IComparable { public int Repetitions; public int Key; public int CompareTo(CountAndKey other) { return Repetitions - other.Repetitions; } } public int? FindLeastFrequentNumber(IList list) { // If list is null or empty, return null if (list == null || list.Count == 0) return null; // Build up a frequency dictionary _fDict.Clear(); foreach (var key in list) { CountAndKey value; if (_fDict.TryGetValue(key, out value)) { value.Repetitions++; } else { _fDict[key] = new CountAndKey { Key = key }; } } // Return the first instance of the minimum frequency dictionary key return _fDict.Values.Min().Key; } }