Author Topic: VBScript - Remove duplicate elements from arraylist  (Read 12534 times)

MatthiasVDE

  • EA User
  • **
  • Posts: 196
  • Karma: +1/-0
    • View Profile
VBScript - Remove duplicate elements from arraylist
« on: March 10, 2017, 04:26:06 am »
Is it possible to remove duplicate elements from an arraylist?
I tried with some functions but no success.

Code: [Select]
'returns an ArrayList without duplicates
function removeDuplicates(arraylist)

dim result
set result = CreateObject("System.Collections.ArrayList")

dim element as EA.Element
for each element in arrayList
dim b
b = contains(arraylist, element)

'if contains then
'
'else
'
'end if


next
set removeDuplicates = result
end function


Code: [Select]
'returns boolean
function contains(arraylist, element)
set contains = false
dim res as EA.Element
for each res in arrayList
if res.ElementID = element.ElementID then
set contains = true
exit for
end if
next


end function

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13256
  • Karma: +554/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: VBScript - Remove duplicate elements from arraylist
« Reply #1 on: March 10, 2017, 06:15:25 am »
You have to iterate backwards through the arraylist and then keep a second arraylist (or dictionary) with the one that you have already encountered.
If you then have a duplicate you can remove it safely from the arraylist.

Another option is to make a new arraylist (or dictionary) object and add only the unique ones.

There are some examples of deleting elements from a list in the VBScript library

Geert

MatthiasVDE

  • EA User
  • **
  • Posts: 196
  • Karma: +1/-0
    • View Profile
Re: VBScript - Remove duplicate elements from arraylist
« Reply #2 on: March 10, 2017, 06:13:48 pm »
I found it:

Code: [Select]
'returns an ArrayList without duplicates
function removeDuplicates(arraylist)
dim result
set result = CreateObject("System.Collections.ArrayList")
dim element as EA.Element
for each element in arrayList
dim b
b = contains(result, element)
if b = false then
result.add(element)
end if
next
set removeDuplicates = result
end function


'returns boolean
function contains(result, element)
contains = false
dim res as EA.Element
for each res in result
if res.ElementID = element.ElementID then
contains = true
exit for
end if
next
end function