Link to home
Start Free TrialLog in
Avatar of scbdpm
scbdpmFlag for United States of America

asked on

Remove duplicate entries from string 'List' array

I have a List that is an array (multiple entries- sorry new to this concept so may not be right words)

list is called xmllist; code below

That list has duplicates.
I would like to remove the duplicate entries.
one of the elements of the list array is a date.
The duplicate is based on this date.

Have tried to iterate thorugh list but getting issues with xmllist.removeat(y).

how can I best do this?

Define list:
Public xmlList As New List(Of String())

Add items to list:
 xmlList.Add({intCount, dtStartDate, strInfo1, strInfo2})

It is startdate that had the duplicates

Open in new window

Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

You can remove the duplicate entries, based on the second value in the array, using code like this:
Dim i As Integer = 0
        Dim DateKeys As New Dictionary(Of String, Object)
        While (i < xmlList.Count)
            If Not DateKeys.ContainsKey(xmlList(i)(1)) Then
                DateKeys.Add(xmlList(i)(1), Nothing)
                i = i + 1
            Else
                xmlList.RemoveAt(i)
            End If
        End While

Open in new window

Avatar of scbdpm

ASKER

Idle_Mind
that works quite well except I failed to add one other component that I'm trying to get this code to do (my fault).
i figured I could adapt the posted code but I'm still having some troubles.

if you could, what I would also need to do is, now that I've identified the duplicate item in xmllist, I need to take one of its compnents and addit to the other item in xmllist that has the same date.

for argument sake, let's say it's xmllist.item(blah)(2).



So you want to take the third item in the original entry, and replace it with the third item in the duplicate entry, and then still delete the whole duplicate entry?
Avatar of scbdpm

ASKER

agree, but working through the code, it seems i points to the item being replace.

how can I tell the item to 'append' to?
Not sure what you're asking for the the append.  Do you want to replace a value in the original array, or add a value from the duplicate array to the original array?

Be specific as to what values you need and I can give you code.

Do you need the order of items in the List to stay the same?  Or can they be re-arranged as long as there are no duplicates?
*The order of the array items within the List would stay the same.
Avatar of scbdpm

ASKER

What I mean is, the code you  provided found an item in the List that had a duplicate date.

there are other elements (not sure if this is correct word) in that item that I need to take out of the duplicate List item and now append/add to the 'original' List item.

What I've done so far is create code that loops throught the List and finds a duplicate.
when it does, it then does the 'append'. Below is code that I used to do that part.
my problem is that the code isn't elegante and was giving errors when the list item as removed.

it doesn't matter if the order of the items in the list stays the same or not..
if it's easier to re-arrange, that would be fine. if not too much more work, would prefer

Dim dtDateHold1 As Date
            Dim dtDateHold2 As Date
            Dim intIndex1 As Int16
            Dim intIndex2 As Int16

            Dim strHold1 As String
            Dim strHold2 As String


            For Each itemArr As String() In xmlList

                intIndex1 = xmlList(x)(0)
                dtDateHold1 = xmlList(x)(1)
                y = 0

                For Each itemArr2 As String() In xmlList
                    intIndex2 = xmlList(y)(0)
                    dtDateHold2 = xmlList(y)(1)

                    'For i As Integer = 0 To itemArr2.Length - 1
                    If dtDateHold2 = dtDateHold1 Then
                        If Not y = x Then


                            ' MessageBox.Show("Same")

                            ''structure
                            'xmlList.Add({intCount, dtStartDate, strHold, varArr(3), varArr(4), varArr(3) & ";" & varArr(1)})
                            ''first take the information from the second item 
                            strHold1 = xmlList.Item(x)(2)
                            strHold2 = xmlList.Item(y)(2)

                            'MessageBox.Show(strHold1 & vbCrLf & strHold2)

                            ''then and combine with first
                            xmlList.Item(x)(2) = strHold1 & vbCrLf & strHold2

                            ''then remove second item
                            ''edit- just store in an array
                            'z = UBound(intRemove)

                            'ReDim Preserve intRemovear(z + 1)
                            'intRemoveArray.Add(y)
                            intRemoveArray.Add({CStr(y), CStr(x)})


                            'xmlList.RemoveAt(y)

                        End If
                    End If
                    'MessageBox.Show((itemArr(x)(1)))
                    'Next i
                    y = y + 1
                Next
                x = x + 1
            Next

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of scbdpm

ASKER

That did it, thanks!

Sorry for taking so long to accept this (these) solution(s) but I was hoping to pick your brain about an error I keep getting.
true to form, I'm not getting it! LOL

it is something like "Unable to copy file "obj\x86\Debug\*.exe" to "bin\Debug\*.exe". The process cannot access the file 'bin\Debug\*.exe' because it is being used by another process."
I've looked in task Manager and the only processes I can identify kill VS2010 when I stop them!

Anyway, thanks for the code!
For some reason the IDE is keeping a hold on the file.  

When in doubt, re-boot.   =\
Avatar of scbdpm

ASKER

The solution I've found is to close out and then reopen VS but that's a pain... it can happen again and again...
when I reopoen VS it's fine for a few times then happens again.

I'll keeep an eye in it, it might be because I've stopped the app while in break mode.

thanks again.