Velocity Reviews > Check for double occurrences

# Check for double occurrences

Boefje
Guest
Posts: n/a

 01-07-2004
Hi,

What I want it simpel. I have got 8 input fields on a form named
title[1] to title[8].

I want to check in a function if there are no double titles. I have
got a solution, but it is plain ugly.

How to achieve this in a elegant way?

G.B.

Brian Genisio
Guest
Posts: n/a

 01-07-2004
Boefje wrote:

> Hi,
>
> What I want it simpel. I have got 8 input fields on a form named
> title[1] to title[8].
>
> I want to check in a function if there are no double titles. I have
> got a solution, but it is plain ugly.
>
> How to achieve this in a elegant way?
>
>
> G.B.

If you had a lot of fields (hundreds, thousands), I would suggest a
O(nln) sort, and a one-time neighbor check O(n), which would result in a
O(nln) solution... it would be the fastest way to accomplish what you
want with a lot of items...

*pseudocode*
sort(fields) -- an O(nln) sort
for i=0 to fields.len - 1
if fields[i] == fields[i+1]
-- In this case, fields[i] and fields[i+1] are the same

But since you only have 8 items, an O(n^2) method will do the trick, and
is likely faster than the O(nln) method, due to overhead :

*pseudocode*
for i=0 to fields.len
for j=i+1 to fields.len
if fields[i] == fields[j]
-- In this case, fields[i] and fields[j] are the same...

Either
Brian

Disco Octopus
Guest
Posts: n/a

 01-07-2004
in news:(E-Mail Removed) m,
Boefje typed:
> Hi,
>
> What I want it simpel. I have got 8 input fields on a form named
> title[1] to title[8].
>
> I want to check in a function if there are no double titles. I have
> got a solution, but it is plain ugly.
>
> How to achieve this in a elegant way?
>
>
> G.B.

this is the result of being so tired that you are even too tired to turn the
pc off and go to bed.......

<html>
<script>
function eatyourpeas(){
var telephonenumber = new Array();

for (i=1;i<9;i++){
eval ("howaboutbeans = document.forms[0].thingy" + i + ".value");
}
}
}
</script>
<body>

<form name="freakenfrackitty">
<input name="thingy1" type="text" value="a"><br>
<input name="thingy2" type="text" value="b"><br>
<input name="thingy3" type="text" value="c"><br>
<input name="thingy4" type="text" value="d"><br>
<input name="thingy5" type="text" value="e"><br>
<input name="thingy6" type="text" value="a"><br>
<input name="thingy7" type="text" value="g"><br>
<input name="thingy8" type="text" value="h"><br>
<input name="fffff" type="button" onclick="eatyourpeas();"><br>

</form>
</body>
</html>

Heiko Fiegen
Guest
Posts: n/a

 01-07-2004
Disco Octopus wrote ...
>in news:(E-Mail Removed) m,
>eval("howaboutbeans = document.forms[0].thingy" + i + ".value");

eval is evil, better use:

regards,
heiko

Thomas 'PointedEars' Lahn
Guest
Posts: n/a

 01-22-2004
Brian Genisio wrote:

> But since you only have 8 items, an O(n^2) method will do the trick,
> and is likely faster than the O(nln) method, due to overhead :
>
> *pseudocode*
> for i=0 to fields.len
> for j=i+1 to fields.len
> if fields[i] == fields[j]
> -- In this case, fields[i] and fields[j] are the same...

Fortunately, access through hashes (in JS: object properties)
allows you to write an algorithm that is O(n):

*pseudocode*
for i := 0 to fields.len
if object[fields[i]]
// dupe
else
object[fields[i]] := value_not_evaluated_as_false

This can solve the "How many times occurs a value as dupe?"
problem as well if you increase the value for object[fields[i]]
by 1 if there is a dupe, and initialize it with 1 if there
is not.

PointedEars