如何从数组中删除重复项? [英] How to remove duplicates from an array?

查看:106
本文介绍了如何从数组中删除重复项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从数组中删除重复的值.

I am trying to remove duplicate values from an array.

我遇到了这个解决方案: http://www.livio.net/main/asp_functions.asp?id = RemDups%20Function

I came across this solution: http://www.livio.net/main/asp_functions.asp?id=RemDups%20Function

如果我通过例如对数组进行硬编码,就可以正常工作

It works fine if I hard code an array, via e.g.

theArray = Array("me@me.com","sid@sid.com","bob@bob.com","other@test.com","other@test.com","other@test.com")

通过livio.net页面上显示的测试步骤删除重复项:

The duplicates are removed via the test steps shown on the livio.net page:

'--- show array before modifications
response.write "before:<HR>" & showArray (theArray)

'---- remove duplicate string values
theArray = RemDups(theArray)

'--- show the array with no duplicate values
response.write "after:" & showArray (theArray)

但是,我正在尝试从表单中输入到文本区域的值中删除重复项.

However, I am trying to remove duplicates from values which are entered into a textarea on a form.

假设我以标准格式获取地址,这些地址之间用逗号分隔,并存储在名为"whotoemail"的字符串中

Assuming I've got the addresses in a standard format where they are comma separated, and are stored in a string called "whotoemail"

因此,"whotoemail"包含:

So, "whotoemail" contains:

me@me.com,sid@sid.com,bob@bob.com,other@test.com,other@test.com,other@test.com

我尝试将数组声明为:

theArray = Array(whotoemail)

然后执行测试步骤-不删除重复项.似乎根本无法识别该数组已被声明,或者它包含任何值.

Then running through the test steps - the duplicates are not removed. It doesn't seem to recognise that the array has been declared at all, or that it contains any values.

然后我想,也许值需要用语音标记包裹,所以我捏造了一个笨拙的方法:

I then thought, maybe the values need to be wrapped in speech marks, so I fudged a clunky way to do that:

testing = Split(whotoemail,",")
loop_address = ""
For i=0 to UBound(testing)
  loop_address = loop_address & "," & chr(34) & trim(testing(i)) & chr(34)
Next

' remove leading comma
left_comma = left(loop_address,1)
if left_comma = "," then
    ttl_len = len(loop_address)
    loop_address = right(loop_address,ttl_len-1)
end if

所以现在,我的"whotoemail"字符串被包裹在语音标记中,就像我对数组进行硬编码时一样.

So now my "whotoemail" string is wrapped in speech marks, just like when I hard coded the Array.

但是仍然不会删除重复的值.

But still the duplicate values are not removed.

声明数组时是否无法动态设置数组的值?

Is it not possible to dynamically set the values of the array when declaring the array?

或者我缺少明显的东西吗?

Or am I missing something obvious?

任何建议将不胜感激.

谢谢!

推荐答案

我将使用

I'd use a dictionary for duplicate elimination, because the keys of a dictionary are by definition unique.

Function RemoveDuplicates(str)
  If Trim(str) = "" Then
    RemoveDuplicates = Array()
    Exit Function
  End If

  Set d = CreateObject("Scripting.Dictionary")
  d.CompareMode = vbTextCompare  'make dictionary case-insensitive

  For Each elem In Split(str, ",")
    d(elem) = True
  Next

  RemoveDuplicates = d.Keys
End Function

这篇关于如何从数组中删除重复项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆