我必须检查ArrayList中的重复记录。在ArrayList中,每个项目在插入新项目之前应该至少有2次。每个例子
Example 1:
AL(0) = '1'
AL(1) = '1'
AL(2) = '2'
AL(3) = '2'
AL(4) = '2'
Method has to return = True, because each value has atleast 2 times in the list.
Example 2:
AL(0) = '1'
AL(1) = '1'
AL(2) = '2'
AL(3) = '3' //Trying to insert new Item, But It should not
Method has to return = 'false', because '2' has 1 time in the list. So I dont
want insert '3' in ArrayList and return false.
答案 0 :(得分:2)
我对VB.Net不太了解,但可能以下C#代码可能有所帮助(使用LINQ)。
array.Distinct().All(item => array.Count(other => other == item) > 1)
我猜测VB语法(可能是错误的)
Array.Distinct().All(Function(item) Array.Count(Function(other) other = item) > 1)
此处数组包含您感兴趣的列表项目
答案 1 :(得分:1)
如果你的ArrayList是有序的,并且包含数字字符串(如你的帖子所示),那么下面的函数应该有效:
Private Function OKToInsertSorted(ByVal theArrayList As ArrayList, _
ByVal stringToInsert As String) As Boolean
With theArrayList
If CInt(stringToInsert) < CInt(.Item(.Count - 1)) Then Return False
If .Count <= 1 Then
If stringToInsert = "1" Then Return True Else Return False
End If
If .Item(.Count - 1).ToString = .Item(.Count - 2).ToString Then
Return True
Else
Return False
End If
End With
End Function
如果您的ArrayList未订购,但仍包含数字字符串(假设您以数字“1”开头),则以下函数应该有效:
Private Function OKToInsertUNSorted(ByVal theArrayList As ArrayList, _
ByVal stringToInsert As String) As Boolean
If stringToInsert = "1" Then Return True
Dim stringToCheck As String = CStr(CInt(stringToInsert) - 1)
Dim qry = From stringItem In theArrayList _
Where stringItem.ToString = stringToCheck _
Group By stringItem Into _
stringCount = Count()
For Each result In qry
If result.stringCount >= 2 Then Return True
Next
Return False
End Function
我根据你所寻找的一些假设在第一个函数中放了相当多的验证码,所以你的里程可能会有所不同。
答案 2 :(得分:0)
对arraylist使用以下检查来检查arraylist中是否有重复数据
return arrList.ToArray().Distinct().Count() == arrList.Count;
如果您想在插入之前检查数据是否已存在于arraylist中,请使用以下检查
if (!arrList.Contains(data)) {
arrList.Add(data);
}
答案 3 :(得分:-1)
您只需检查数组列表计数和不同计数即可。
var isDuplicate = arrayList.Count() > arrayList.Distinct().Count();