Solved

Remove duplicates from array

Posted on 2006-10-26
4
241 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]
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

762 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

6 Experts available now in Live!

Get 1:1 Help Now