Arrays 对数组中一次出现的唯一元素进行计数
我有这个问题Arrays 对数组中一次出现的唯一元素进行计数,arrays,vb.net,linq,Arrays,Vb.net,Linq,我有这个问题 Structure Person Dim name As String Dim age As Integer End Structure Dim people(10) As Person people(0).name = "A" people(0).age = 21 ... people(9).name = "Z" people(9).age = 30 现在,例如,如果我对age={20,21,20,23,25,35,30,29,25,26}有以下值 我正试图
Structure Person
Dim name As String
Dim age As Integer
End Structure
Dim people(10) As Person
people(0).name = "A"
people(0).age = 21
...
people(9).name = "Z"
people(9).age = 30
现在,例如,如果我对age={20,21,20,23,25,35,30,29,25,26}有以下值
我正试图从具有唯一年龄的people
中识别并排除每个人,即,我的最终people
数组应仅包含年龄20,25
的人的记录
我试过这样的方法:
tempList() As Int
cleanList() As Int
for i = 0 to people.count - 1
array.resize(templist, i + 1)
templist(i) = people(i).age
next
cleanList = tempList.Distinct().ToArray()
问题是,这样做会给我数组中每个不同的值。但我只想要那些只出现一次的值
有什么帮助吗
非常感谢 您需要首先对年龄列表进行分组,以仅获取重复值:
Dim ages= From a In allages _
Group a By Key = a Into Group _
Where Group.Count() > 1 _
Select Group.Key
然后,您可以筛选您的人员列表:
Dim result = From p In people _
Where ages.Contains(p.age) _
Select p
试试这个:
Dim duplicatePersons = people.Where(Function(p) people.Count(Function(p1) p1.Age = p.Age) > 1).ToArray()
您可以按
.age
分组,并从包含多个项目的组中获取项目:
Dim cleanList = people.ToLookup(Function(p) p.age).
Where(Function(g) g.Count > 1).SelectMany(Function(g) g).ToList