1

If i had an array such as -

array(0) = 85
array(1) = 85
array(2) = 53
array(3) = 203
array(4) = 85

How can I make a new array with no double values? so the array would be

array(0) = 85
array(1) = 53
array(2) = 203
kurupt_89
  • 1,542
  • 8
  • 37
  • 65

4 Answers4

3

I know this is old question but recently I came across same scenario where i need to remove redundant entires from array. I searched SO but not found any simple method to do same.After searching i got simple method on Hey, Scripting Guy! Blog.

From this link

Set objDictionary = CreateObject("Scripting.Dictionary")

arrItems = Array("a","b","b","c","c","c","d","e","e","e")

For Each strItem in arrItems
  If Not objDictionary.Exists(strItem) Then
      objDictionary.Add strItem, strItem   
  End If
Next

intItems = objDictionary.Count - 1

ReDim arrItems(intItems)

i = 0

 For Each strKey in objDictionary.Keys
   arrItems(i) = strKey
   i = i + 1
 Next

For Each strItem in arrItems
   Wscript.Echo strItem
 Next

Hope this will help some one

Community
  • 1
  • 1
Amol Chavan
  • 3,835
  • 1
  • 21
  • 32
  • 1
    This is the only solution that I found that works... after replacing the Wscript.Echo with Response.Write. – WilliamK Sep 20 '22 at 07:51
1

Using the function from my answer to your previous question with test code like

  Dim aA   : aA   = Split( "85 459 459 90 85 85 85" )
  Dim aRes : aRes = diffArray( aA, Array() )
  WScript.Echo "A  :", Join( aA )
  WScript.Echo "UNI:", Join( aRes( 0 ) ), "settifieds"

gives you:

A  : 85 459 459 90 85 85 85
UNI: 85 459 90 settifieds
Community
  • 1
  • 1
Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96
0

I don't know if there is a function in vbs to remove duplicate element from array.If it not exists your can use the following.May be problem with the syntax but you can use the strategy

length=5
    newLength=1
    for i=0 to length-1

      for j=0 to newLength-1
      if(array(i)=array(j)) then
    exit For
    end if
      next
      if(j=newLength) then
    array(newLenth++)=array(i);
    end if

    next
Rasel
  • 15,499
  • 6
  • 40
  • 50
0

You can also do it in n log n if you sort the array.

Thom Smith
  • 13,916
  • 6
  • 45
  • 91
  • Can you show and example please or a link to something that shows an example where I can learn how to do that? – kurupt_89 Jul 06 '11 at 05:40
  • 1
    If you sort the array still you have to remove the duplicate and have to use the method below.So thinking about sorting is not good idea here,I think – Rasel Jul 06 '11 at 06:21
  • +1 for pointing out that sorting is not needed for dups removal. – Ekkehard.Horner Jul 06 '11 at 07:38