The language I'm using at the moment is VBA, for context. I'm hoping to occasionally post things like this, to learn best practice and give others something to see to help learn too. I hope that intent made sense?
Anyway, here's a function I built that I'm fairly happy with. I don't know that there's a built-in way to do this in VBA, but maybe there is a better way to do it all the same. This is RemoveDuplicates, which accepts a string (which should be a list of items delimited by a semicolon) and splits it, then compares the two and returns a string (with the items delimited by a semicolon) without duplicates.
I built this for an access DB that stores email addresses. We're pulling email addresses based on other criteria, and sometimes can pull the same person twice. This is intended to remove duplicates after retrieving addresses.
Private Function RemoveDuplicates(strDupes As String) As String Dim astrSplitList() As String Dim astrFilteredList() As String Dim intBaseIterator As Integer, intInternalIterator As Integer Dim boolFound As Boolean astrSplitList = Split(strDupes, ";") ReDim astrFilteredList(0 To 0) For intBaseIterator = 0 To UBound(astrSplitList) For intInternalIterator = 0 To UBound(astrFilteredList) If astrSplitList(intBaseIterator) = astrFilteredList(intInternalIterator) Then boolFound = True Next If Not boolFound Then ReDim Preserve astrFilteredList(UBound(astrFilteredList) + 1) astrFilteredList(UBound(astrFilteredList)) = astrSplitList(intBaseIterator) End If boolFound = False Next RemoveDuplicates = Join(astrFilteredList, ";") End Function
Didn't put this in the .NET forum initially cause I'm not concerned with the language as much as the code structure. . .
This post has been edited by depricated: 24 October 2012 - 08:56 AM