Hello all,
Sorry for such a beginner question, but I can't seem
to work through this. I've thought about it for a while, and I just
can't seem to figure out a solution. Sorry in advance:
I've got an array with a bunch of names in it, and I
want to sift through it to make sure that none of the names are the
same, and if they are, then I want to take out one of the matches from
that pair.
So for instance, if i've got an array that contains the names:
Robert
Rob
Jacob
Jack
Jacob
I want to be able to go through and find that second jacob to take it out, so that the array only contains the names
Robert
Rob
Jacob
Jack
Again, sorry for such a mundane and dorky quesion, but I simply have no idea as to how to attack it!
Any help is greatly, greatly appreciated
thanks so much!
-Robert

Sifting through array for matches
Yuval124
If you pass the contents of your arrays to an instance of this class, and then pull them out, there will be no duplicates. If you use this container in general to start with, then you'll get no duplicates right away.
Rob B
"names are the same, and if they are, then I want to take out one of the matches from that pair"
I have a favorite old trick for this which relies on a property of dictionaries. They will not allow duplicate keys. Soooooo (critten from memory)
Dim Dic as new Dictionary (oftype String, integer)
'Iterating through your list.....
For i as integer = 0 to list.length-1
Try
Dic.add(list(i),i) ' if a dup name is added the dictionary will throw an exception
'if needed do non-duplication processing here
catch
'if needed do duplication processing here
end try
Next
ASR
Unfortunately, I don't believe that VB.NET has a set collection. You can easily create one though:
Public
Class SIList(Of T) Inherits System.Collections.Generic.List(Of T)
Public Overloads Sub Add(ByVal item As T) If Not Contains(item) Then MyBase.Add(item) End If End SubEnd
ClassIt's called SIList for SingleInstanceList. Set is taken :-) This will reject any duplicate matches. Using Dictionary to do the same means dealing with exceptions, or writing code to see if the set contains a key, which you may as well do with the List instead. This way, you encapsulate the behaviour you want, which is what OO is all about.
Breakgate
Public Class SIList(Of T)
Inherits System.Collections.Generic.List(Of T)
Public Overloads Sub Add(ByVal item As T)
If Not Contains(item) Then
MyBase.Add(item)
End If
End Sub
End Class
I got the code to you know... compile. Just that code alone, but I'm pretty much a clueless nube (for lack of a better term), and I'm not at all sure where to... you know give it my array. So if the array is called personarray... where do i tell it... you know, sift through this buddio
Sorry for being so clueless
any help is greatly appreciated
Thanks in advance
-Robert
arturm
Jason Holt
Public Class SIList(Of T)
Inherits System.Collections.Generic.List(Of T)
Public Overloads Sub Add(ByVal item As T)
If Not Contains(item) Then
MyBase.Add(item)
End If
End Sub
End Class
If you add this to your project somewhere, then you can create instances of it like this:
Dim words as SIList(of String) = new SIList(Of String)
Now you have a container that you can add strings to, but it will only add strings that are not already there. So all you have to do is use this in place of your array, and it will never contain duplicate values. Or, if you have arrays, you can do this
Dim words as SIList(of String) = new SIList(Of String)
Dim s as string
for each s in array1
words.Add(s)
next
for each s in array2
words.Add(s)
next
I believe that words will then have a ToArray() method which will return a new string array, with no duplicate values.
Fregas
Sorry about being consistantly annoying and beginerish. However, I simply cannot figure out what to do with that code that was provided!
I'm not really a good programmer, nor do I know much... and I'm not really sure how to make the code suggested by Renee sift through an array. I'm sorry for making you spoon feed me, but i'm affraid that's the only way that I'm going to be able to understand this.
So sorry, any help really is appreciated!
Thanks so much
-Robert
StuartMiller
If you look at the code Renee wrote, it catches an exception. The Dictionary throws an exception if you insert a duplicate key. My first thought was to offer code like this ( as an exception will slow down the code ), then I thought it better to wrap it, so all the user needs to do is insert items.