469,319 Members | 2,398 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,319 developers. It's quick & easy.

arrays

I have a collection of objects that may have duplicates in it. What I
want to do is to iterate through the list and only add the non
duplicates to an array, so as I am adding to the array, I need some way
of checking against the current contents of the array. Is this
possible, or is there a better way to do this?

*** Sent via Developersdex http://www.developersdex.com ***
Aug 8 '08 #1
2 1208
you could use a dictionary and the 'exists' method

The following examples illustrate the use of the Exists method.

[JScript]
function keyExists(k)
{
var fso, s = "";
d = new ActiveXObject("Scripting.Dictionary");
d.Add("a", "Athens");
d.Add("b", "Belgrade");
d.Add("c", "Cairo");
if (d.Exists(k))
s += "Specified key exists.";
else
s += "Specified key doesn't exist.";
return(s);
}[VBScript]
Function KeyExistsDemo
Dim d, msg ' Create some variables.
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "Athens" ' Add some keys and items.
d.Add "b", "Belgrade"
d.Add "c", "Cairo"
If d.Exists("c") Then
msg = "Specified key exists."
Else
msg = "Specified key doesn't exist."
End If
KeyExistsDemo = msg
End Function
Aug 8 '08 #2
Mike P wrote:
I have a collection of objects that may have duplicates in it. What I
want to do is to iterate through the list and only add the non
duplicates to an array, so as I am adding to the array, I need some
way of checking against the current contents of the array. Is this
possible, or is there a better way to do this?
There's no shortcut. You need to do a nested loop.
start loop through collection
within the collection loop, loop through the array checking to see if object
already exists.
if not, add the object to the array and move to the next object

Do you need more detail than that? if so, we sill need more details.

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Aug 8 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

21 posts views Thread by Matteo Settenvini | last post: by
5 posts views Thread by JezB | last post: by
3 posts views Thread by Michel Rouzic | last post: by
1 post views Thread by Rob Griffiths | last post: by
41 posts views Thread by Rene Nyffenegger | last post: by
6 posts views Thread by Robert Bravery | last post: by
16 posts views Thread by mike3 | last post: by
weaknessforcats
29 posts views Thread by weaknessforcats | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by mdpf | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.