Find distinct unique values from array using QTP/vbscript

Hello Friends,

In this post, we will see, how to get unique/distinct values from an array.
We will do that using very useful and optimum feature of QTP – Dictionary Object.

Here is the code–

Dim objDict, oldArray, val
oldArray = Array (“11”, “21”, “11”, “31”, “21”, “41”, “31”)

Set objDict = CreateObject(“Scripting.Dictionary”)
objDict.CompareMode = vbTextCompare

For each val in oldArray
objDict(val) = val
Next

newArray = objDict.Items

For i=0 to ubound(newArray)
msgbox newArray(i)
Next

…n done!!!

Plz do post your queries/comments…

2 thoughts on “Find distinct unique values from array using QTP/vbscript”

  1. Hi,

    Need small change in below part of above mentioned code…

    Incorrect One :
    For i=0 to ubound(newArray)
    msgbox newArray(i)
    Next

    Correct One :
    For i=0 to ubound(newArray)-1
    msgbox newArray(i)
    Next

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top