12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Common.Helper
- {
- public class JsHelper
- {
- /// <summary>
- /// 计算使用最多的数字
- /// </summary>
- /// <param name="list">字符串集合</param>
- /// <param name="number">需要几位</param>
- /// <returns></returns>
- public static IEnumerable<string> UseMax(List<string> list, int number = 3)
- {
- list = list.Where(p => !string.IsNullOrEmpty(p)).ToList();
- var dict = new Dictionary<string, int>();
- var group = list.GroupBy(p => p).ToList();
- group.ForEach(p =>
- {
- dict.Add(p.Key, list.Where(q => q == p.Key).Count());
- });
- var dictOrder = dict.OrderByDescending(p => p.Value);
- if (list.Count > 0)
- {
- number = dictOrder.Count() > number ? number : dictOrder.Count();
- var i = 1;
- foreach (var item in dictOrder)
- {
- if (i <= number)
- yield return item.Key;
- i++;
- }
- }
- }
- /// <summary>
- /// 计算使用最多的数字
- /// </summary>
- /// <param name="list">字符串集合</param>
- /// <param name="number">需要几位</param>
- /// <returns></returns>
- public static string UseMaxString(List<string> list, int number = 3)
- {
- return UseMax(list, number).Join();
- }
- }
- }
|