开发者

Remove double values from array classic asp

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


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


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


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜