Sign In | Register Now About Bytes | Help | Site Map
Connecting Tech Pros Worldwide

arrays

Question posted by: Mike P (Guest) on August 8th, 2008 02:35 PM
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 ***
Bob Barrows [MVP]'s Avatar
Bob Barrows [MVP]
Guest
n/a Posts
August 8th, 2008
02:55 PM
#2

Re: arrays
Mike P wrote:
Quote:
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"



Jon Paal [MSMD]'s Avatar
Jon Paal [MSMD]
Guest
n/a Posts
August 8th, 2008
02:55 PM
#3

Re: arrays
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



 
Not the answer you were looking for? Post your question . . .
189,814 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
  • Didn't find the answer you were looking for?
    Post Your Question
  • Top Community Contributors