Check for double occurrences

B

Boefje

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?

Thans for your time.

G.B.
 
B

Brian Genisio

Boefje said:
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?

Thans for your time.

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 == fields[i+1]
-- In this case, fields 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 == fields[j]
-- In this case, fields and fields[j] are the same...

Either
Brian
 
D

Disco Octopus

in 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?

Thans for your time.

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();
var howaboutbeans = new String();

for (i=1;i<9;i++){
eval ("howaboutbeans = document.forms[0].thingy" + i + ".value");
if (telephonenumber[howaboutbeans] == howaboutbeans) {
alert ("Found duplicate " + howaboutbeans);
}
telephonenumber[howaboutbeans] = howaboutbeans;
}
}
</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>
 
H

Heiko Fiegen

Disco Octopus wrote ...
in eval("howaboutbeans = document.forms[0].thingy" + i + ".value");

eval is evil, better use:
howaboutbeans = document.forms[0].elements["thingy" + i].value;

regards,
heiko
 
T

Thomas 'PointedEars' Lahn

Brian said:
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 == fields[j]
-- In this case, fields 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]
// dupe
else
object[fields] := 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]
by 1 if there is a dupe, and initialize it with 1 if there
is not.


PointedEars
 

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,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top