3
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?

2
  • 2
    I wouldn't use an ArrayList ever (well, maybe there would be some strange situation where I would). Better to use a List(Of String) in this case. Commented May 31, 2011 at 9:39
  • Unfortunately, sometimes we are stuck supporting legacy code, i.e. .Net 1.1. Commented Aug 8, 2013 at 19:59

6 Answers 6

9

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
Sign up to request clarification or add additional context in comments.

Comments

5

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:

If Not list.Contains(number) Then
    list.Add(number)

Comments

4

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:

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().

2 Comments

Why is this not #1?
Distinct is not a member of System.array
1
        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++;
            }
        }

Comments

0

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:

    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

Comments

0

Why don't you check like this

if number.length = 11 then
    if Not ItemList.contains(number)
        ItemList.Add(number)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.