Vb.net 如何从数组中删除重复项?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6185149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Vb.net How to remove duplicates from an Array?
提问by Beginner
Dim ItemList As New ArrayList()
For i = 0 To dgExtract.Items.Count - 1
gRow = dgExtract.Items(i)
chk = gRow.FindControl("chkSelect")
If chk.Checked Then
sEmail = gRow.Cells(7).Text
dim number as string = Regex.Replace(sEmail,"[^0-9]","")
if number.length = 11 then
ItemList.Add(number)
end if
end if
Next
I build up the ItemList array with the above code. How do i remove any duplicates in this array?
我用上面的代码构建了 ItemList 数组。如何删除此数组中的任何重复项?
回答by Saurabh
Setting:
环境:
Dim number As Integer
Dim num As String
Dim al As New ArrayList()
If Not (al.Contains(number)) Then
al.Add(number)
End If
Getting:
获得:
For Each number In al
num = number.ToString()
Next
回答by Srinivasan__
Rather than checking and removing the duplicate elements you can check whether it is in the array, if it doesn't exist you can add to array, else do nothing.
您可以检查它是否在数组中,而不是检查和删除重复元素,如果它不存在,您可以添加到数组中,否则什么都不做。
Declare a List<string>
object named for instance list
. In the loop:
声明一个List<string>
名为 instance的对象list
。在循环:
If Not list.Contains(number) Then
list.Add(number)
回答by Neverbirth
You'd declare some array (or List, or whatever collection you may prefer) and would do something like:
您将声明一些数组(或列表,或您可能喜欢的任何集合)并执行以下操作:
Array.Resize(numberArray, numberArray.Length + 1)
numberArray[numberArray.Length - 1] = number
Then you could use LINQ:
然后你可以使用LINQ:
numberArray.Distinct()
And then, iterate the array and do whatever you need.
然后,迭代数组并执行您需要的任何操作。
EDIT: Better what Srinivasan__ said, check whether the item exists, and if it doesn't add it. To check for it, you can use Exists(). Or if using something like a List, Contains().
编辑:Srinivasan__ 所说的更好,检查该项目是否存在,如果不添加它。要检查它,您可以使用 Exists()。或者,如果使用类似列表的内容,Contains()。
回答by dhinesh
int i;
int j;
int count = 0;
int[] list = new int[5];
//to add 5 data with duplicate
list[0] = 1;
list[1] = 5;
list[2] = 2;
list[3] = 4;
list[4] = 5;
#region toremovetheduplicate
int c = 0, flag = 0;
int[] list1 = new int[5];
for (i = 0; i < list.Length; i++)
{
flag = 0;
for (j = i + 1; j < list.Length; j++)
{
if (i != j)
{
if (list[i] == list[j])
{
flag = 1;
}
}
}
if (flag == 0)
{
list1[c] = list[i];
c++;
}
}
回答by user3144575
I just saw this, I tried the following code ; it works fine, I'm using a CheckedListBox to view results. There 2 arraylist used. 'Darray' holding the original list of duplicated strings. Then, 'FinArray' to dump the non-duplicated strings, then display the 'FinArray' contents in the listbox:
我刚看到这个,我尝试了以下代码;它工作正常,我正在使用 CheckedListBox 查看结果。使用了2个arraylist。'Darray' 保存重复字符串的原始列表。然后,'FinArray' 转储非重复字符串,然后在列表框中显示 'FinArray' 内容:
Sub CleanDupes()
' Clear listbox
CheckedListBox1.Items.Clear()
' Create Final Array for non-duped data
Dim FinArray As New ArrayList
Dim InitFinarray, DarrayN, FinArrayN As String
' Add first record from original array into new array
FinArray.Add(Darray.Item(0))
InitFinarray = FinArray.Item(0)
CheckedListBox1.Items.Add("Select/Unselect All")
CheckedListBox1.Items.Add(InitFinarray)
' Loop into Orig Array and compare each record with strings in new array,
' if exist in new array, then skip, else add it
For n As Integer = 0 To Darray.Count - 1
DarrayN = Darray.Item(n)
For n2 As Integer = 0 To FinArray.Count - 1
If FinArray.Contains(DarrayN) Then
Else
FinArray.Add(DarrayN)
FinArray.Sort(1, FinArray.Count - 1, Nothing)
End If
Next
Next
'Display New Non-Duped Array in listbox
For n3 As Integer = 1 To FinArray.Count - 1
CheckedListBox1.Items.Add(FinArray(n3))
Next
End Sub
回答by Developer
Why don't you check like this
你为什么不这样检查
if number.length = 11 then
if Not ItemList.contains(number)
ItemList.Add(number)