Unity List相关问题

1、list随机数值,重复的数量不超过指定大小。

复制代码
using System.Linq;
private List<int> iconIndexs;
for (int i = 0; i < 5; i++)
{
   int newIndex = Random.Range(0, 3);
   // 检查列表中已有的相同元素的数量
   int count = iconIndexs.Count(x => x == newIndex);
   // 如果已有相同元素的数量超过等于2个,则重新生成一个不同的值
   while (count >= 2)
   {
      newIndex = Random.Range(0, 3);
      count = iconIndexs.Count(x => x == newIndex);
   }
   iconIndexs.Add(newIndex);
}

2、获取list集合中,相同数量最少的一个元素

复制代码
using System.Linq;
private int CalculateItemScroe(List<int> items)
{
   var groups = items.GroupBy(x => x);
   var minGroup = groups.OrderBy(g => g.Count()).First();
   int minElement = minGroup.Key;
   int minCount = minGroup.Count();
   //Log.Info($"最少的元素是 {minElement},出现次数为 {minCount}");
}
相关推荐
靠近彗星2 小时前
2.1线性表
数据结构
island13142 小时前
【Redis#9】其他数据结构
数据结构·数据库·redis
nsjqj3 小时前
数据结构:优先级队列(堆)
数据结构
JasmineX-14 小时前
数据结构——顺序表(c语言笔记)
c语言·开发语言·数据结构·笔记
小六子成长记4 小时前
【C++】:list容器全面解析(超详细)
c++·windows·list
I'm a winner5 小时前
第五章:Python 数据结构:列表、元组与字典(一)
开发语言·数据结构·python
D.....l5 小时前
冒泡排序与选择排序以及单链表与双链表
数据结构·算法·排序算法
欧阳天风5 小时前
链表运用到响应式中
javascript·数据结构·链表
靠近彗星7 小时前
2.3单链表
数据结构