473,386 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Check for double occurrences

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.
Jul 20 '05 #1
4 1370
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?

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[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

Jul 20 '05 #2
in news:28*************************@posting.google.co 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?

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>


Jul 20 '05 #3
Disco Octopus wrote ...
in news:28*************************@posting.google.co m,
eval("howaboutbeans = document.forms[0].thingy" + i + ".value");


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

regards,
heiko

Jul 20 '05 #4
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
Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Lodewijk Smit | last post by:
Hi, In the C standard of 1999 additional mathematical functions are added. For example, beside the traditional double sin(double x); there are also the functions: float sinf(float x);...
1
by: Tom | last post by:
I can't get the Microsoft Jet OLEDB 4.0 driver to read a tab delimited text file that has double quotes imbedded in the data. For example Field1 | Field2 | Field3 Bob | Pant Size 34" Waist | 22...
9
by: RJ | last post by:
Hi All, Suppose i have a double pointer, how do I check the content of that pointer is a 8/16/24/32 bit data? Any suggestion!! Thanks, Raja
32
by: ma740988 | last post by:
template <class T> inline bool isEqual( const T& a, const T& b, const T epsilon = std::numeric_limits<T>::epsilon() ) { const T diff = a - b; return ( diff <= epsilon ) && ( diff >= -epsilon );...
19
by: Erich Pul | last post by:
hi! i got a structure, which should be filled with random integer values (it is in fact a generator for numbers like in a lotto), but these values must not be recurring, so no double occurrences...
2
by: shuisheng | last post by:
Dear All, Assume I have a class for a cuboid domain. The domain is defined by the cuboid's lower corner, such as (0, 0, 0), and upper corner, such as (1, 1, 1). The upper corner should be always...
3
by: Beeteh | last post by:
Hello everybody! I'm a so-called newbie at programming in C/C++ and I'm currently working on a poject that searches for a particular string in a file. The user enters the string he/she is searching...
1
by: test2000 | last post by:
Sorry, I'm new to RegExp. I couldn't solve the problem myself. That's why I dare to ask here for a little advice... ;-) What I want to achieve: return all occurrences of L were x is one or more...
5
by: herman | last post by:
How can I replace all occurrences of a character with another character in std string? For example, I want to replace '/' with '+' in my std::string I have looked at the replace() method in...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.