Solved

Remove duplicates from array

Posted on 2006-10-26
4
242 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 142

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

930 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now