Wednesday, March 6, 2013

How to delete duplicate values in an array

The following script works both for integers and straing arrays.

Rather than using two many for loops and complex logic, it can be done with Dictionary and another Array object.

dim tempArr()
'arrName=Array(5,5,5,5,12,10,15,10,125,5,1,1,2,3,4,5,6,7,8,8,8,8,8,8,8)
arrName=Array("uday","naveen","kiran","usha","uday","vani","usha")
set d=createobject("Scripting.Dictionary")
n=0
For i = 0 To UBound(arrName)
        If Not d.Exists(arrName(i)) Then
     redim preserve tempArr(n)
            d.Add arrName(i), arrName(i)
            tempArr(n) = arrName(i)
     n = n + 1
        End If
Next
arrName = tempArr
for i=0 to ubound(arrName)
 msgbox arrName(i)
next