Solved

Remove duplicates from array

Posted on 2006-10-26
4
245 Views
Last Modified: 2010-04-30
I have a program that sends out emails to a generated list separated by semi-colons.  There are never more than 5 addresses but I want to be sure that I don't have any duplicates.  I can load them into an array using split(emails, ";").  How do I sort and remove duplicates?

Thanks,

Traigo
0
Comment
Question by:traigo
4 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17817264
from the array I would add them to a collection;


dim email as string
dim emails() as string
dim col as collection
dim l as long

email = "a@email.com;b@email.com;a@email.com;c@email.com"
emails = split(email, ";")

set col = new collection
on error resume next
for l = lbound(emails) to ubound(emails)
  email = emails(l)
   'as we have error resume next active, this will ensure that the code continues, although we might try to add a duplicate key here
  col.add email, email
next


here you have a collection of unique email addresses

0
 
LVL 35

Accepted Solution

by:
Raynard7 earned 500 total points
ID: 17817265
Hi,
I'd use a dictionary (from the microsoft scripting runtime libary)

dictionaries have an exists property for their keys

for a new dictionary I would use

dim dicCheck as new dictionary

I'd split the emails into an array - then foreach element in the array check to see if it is in the array - and if it is then do not add it again

ie

if dicCheck.exists(arrayElement) = false then
    dicCheck.Add arrayElement, 1
end if

then to put everything back out i would do foreach element in dicCheck.Keys and output this into one string
0
 
LVL 19

Expert Comment

by:BrianGEFF719
ID: 17817311
Maybe try something like:

dim emails() as string
dim emailList as string
dim i as long, j as long
dim dupe as boolean

emailList = "a@email.com;b@email.com;a@email.com;c@email.com"
emails = split(emailList,";")
do
dupe = false
for i = lbound(emails) to ubound(emails)
 for j = lbound(emails) to ubound(emails)
  if ((emails(i) = emails(j) )and (i <> j)) then
   emails(i) = ""
   dupe = true
   exit for
 end if
 next j
next i
loop while (dupe)
0
 

Author Comment

by:traigo
ID: 17817338
Thanks for the quick response.  I got it working with raynard7's suggestion.

Dim Dict As Dictionary
Set Dict = New Dictionary
arr = split("me@domain.com;me@domain.com;you@domain.com;me@domain.com", ";")

dim i as long

for i=0 to ubound(arr)
      if dict.exists(arr(i)) = false Then
            dict.add arr(i), 1
      End If
next

dim elem as variant
dim str as string
str = ""
for each elem in dict.keys
str = str & elem & ";"
next

str = Left(str, len(str) - 1)
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question