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); } public class Solution : ICodeRush { public int? FindLeastFrequentNumber(IList list) { if (list == null || list.Count == 0) return null; int b = list.Count; if (b < 3) return list[0]; if (b < 100) { int c = int.MaxValue, d = 0; for (int i = 0; i < b; i++) { int e = 0, f = list[i]; for (int j = i; j < b; j++) if (f == list[j]) e++; if (e == 1) return f; if (e < c) { c = e; d = f; } } } var a = new Dictionary(); var g = new int[5000]; bool h = list == null, k = h; int l = g.Length, m = g[2666]; for (int i = 0; i < list.Count; i++) { int n = list[i]; if (n > -1 && n < 5000){ g[n]++; k = true; if (l > n) l = n; if (m < n) m = n + 1; } else { if (a.ContainsKey(n)) a[n]++; else a.Add(n, 1); h = true; } } int o = int.MaxValue, p = 0; if (k) { for (int i = l; i < m; i++) { if (g[i] < o) { o = g[i]; if (o == 1) return i; p = i; } } } if (h) { foreach (var q in a) { if (q.Value < o) { o = q.Value; if (o == 1) return q.Key; p = q.Key; } } } return p; } }