Checking for duplication in an array or delimited string

C

CJM

In an application I'm working the user has the opportunity to record the
despatching of one or more items with serial numbers.

For each item they despatch, they have to chose the serial no that they want
to despatch from a list of available ones. In many cases, these items will
be of the same time, so the dropdown of available serial no's may be the
same.

So now I need to check that the user hasnt picked the same serial no for two
items....

The serial no fields all have the same name, so when doing the server side
validation I'll be presented with a delimited string as a result. What is
the best way to make sure that none of these serial numbers are duplicated.
I havent thought of a specific solution yet, but I imagine you could use
InStr or Arrays to bully the result out, or perhaps enter the values into a
recordset and manipulate them that way; I havent got as far as figuring out
any kind of sultion yet, but I figured that someone on the NG will have
already thought of a quick & efficient approach - I reckon this must be a
reasonably common issue out there...

Thanks in advance

Cheers
 
M

McKirahan

CJM said:
In an application I'm working the user has the opportunity to record the
despatching of one or more items with serial numbers.

For each item they despatch, they have to chose the serial no that they want
to despatch from a list of available ones. In many cases, these items will
be of the same time, so the dropdown of available serial no's may be the
same.

So now I need to check that the user hasnt picked the same serial no for two
items....

The serial no fields all have the same name, so when doing the server side
validation I'll be presented with a delimited string as a result. What is
the best way to make sure that none of these serial numbers are duplicated.
I havent thought of a specific solution yet, but I imagine you could use
InStr or Arrays to bully the result out, or perhaps enter the values into a
recordset and manipulate them that way; I havent got as far as figuring out
any kind of sultion yet, but I figured that someone on the NG will have
already thought of a quick & efficient approach - I reckon this must be a
reasonably common issue out there...

Thanks in advance

Cheers


The Dictionary object will work as each key must be unique.

Dim objDIC
Set objDIC = Server.CreateObject("Scripting.Dictionary")


Test to see if the serial number exists and, if not, add a serial number to
the Dictionary.

If Not objDic.Exists(serialnumber) The
objDIC.Add serialnumber, ""
Else
'* duplicate serialnumber processing
End If


Finally, when you're through, do some clean up:

Set objDIC = Nothing
 
T

Thomas

split the delimited string to an array.

then either use the dictionary like mckirahan suggested, or just do this:

dim i, strCheck

strCheck = " "
for i = 0 to ubound(myarray) - 1
if instr(strCheck, " " & myarray & "; ") = 0 then
strCheck = strCheck & myarray & "; "
else
' duplicate serial
end if
next

of course with large arrays this is not performant at all... :)
 
C

CJM

McKirahan said:
The Dictionary object will work as each key must be unique.

Dim objDIC
Set objDIC = Server.CreateObject("Scripting.Dictionary")


Test to see if the serial number exists and, if not, add a serial number
to
the Dictionary.

If Not objDic.Exists(serialnumber) The
objDIC.Add serialnumber, ""
Else
'* duplicate serialnumber processing
End If

I did actually look at the dictionary object, but never came to a
conclusion, but this solution looks to be both efficient and effective...

Thanks
 
M

Mark Schupp

There are many ways that you could check for duplication each has its pros
and cons and the performance for each will vary with the number and type of
the data to be checked.

One is the dictionary object as mentioned.

If the list is very short then a "brute force" search (also already
mentioned) may have better performance than dictionary because there would
be no overhead for the dictionary object creation.

For longer lists you could build an array in sorted order and do a binary
search for duplicate values (look for "binary search" and "insertion sort"
algorithms).

If the key values are numeric and always in a restricted range then you
could also build an array where the key value or a simple derivative of the
key is the index. This approach would be neater with JScript because JScript
arrays are dynamic and sparse (only use memory for elements that have
values).

You could conceivably build a script object that would pick the "best"
method based on the number of elements in your original list.
 
C

CJM

Mark Schupp said:
There are many ways that you could check for duplication each has its pros
and cons and the performance for each will vary with the number and type
of the data to be checked.

One is the dictionary object as mentioned.

If the list is very short then a "brute force" search (also already
mentioned) may have better performance than dictionary because there would
be no overhead for the dictionary object creation.

For longer lists you could build an array in sorted order and do a binary
search for duplicate values (look for "binary search" and "insertion sort"
algorithms).

If the key values are numeric and always in a restricted range then you
could also build an array where the key value or a simple derivative of
the key is the index. This approach would be neater with JScript because
JScript arrays are dynamic and sparse (only use memory for elements that
have values).

You could conceivably build a script object that would pick the "best"
method based on the number of elements in your original list.

--
--Mark Schupp
Head of Development
Integrity eLearning
www.ielearning.com

mark,

I figure that in the majority of cases the brute force approach would
technically be the quickest since we are dealing with v. small lists here.
However, I've opted for the dictionary approach, because a) the overhead is
not significant enough to affect this low-ish volume system, and b) I like
it...

Cheers

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top