• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 268
  • Last Modified:

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

0
scbdpm
Asked:
scbdpm
  • 5
  • 5
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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

0
 
scbdpmAuthor Commented:
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).



0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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

how can I tell the item to 'append' to?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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.
0
 
scbdpmAuthor Commented:
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

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Ok...looking at your code:

    xmlList.Item(x)(2) = strHold1 & vbCrLf & strHold2

It appears to me that you want to take the third entry from the dupe and append it to the third entry of the original with a vbCrLf in-between.

Try this out:
Dim i As Integer = 0
        Dim key As String
        Dim DateKeys As New Dictionary(Of String, String())
        While (i < xmlList.Count)
            key = xmlList(i)(1)
            If Not DateKeys.ContainsKey(key) Then
                DateKeys.Add(key, xmlList(i))
                i = i + 1
            Else
                DateKeys(key)(2) = DateKeys(key)(2) & vbCrLf & xmlList(i)(2)
                xmlList.RemoveAt(i)
            End If
        End While

Open in new window

0
 
scbdpmAuthor Commented:
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!
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
For some reason the IDE is keeping a hold on the file.  

When in doubt, re-boot.   =\
0
 
scbdpmAuthor Commented:
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.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now