Check for double occurrences

Discussion in 'Javascript' started by Boefje, Jan 7, 2004.

  1. Boefje

    Boefje Guest

    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.
    Boefje, Jan 7, 2004
    #1
    1. Advertising

  2. 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 == 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
    Brian Genisio, Jan 7, 2004
    #2
    1. Advertising

  3. in news:,
    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>
    Disco Octopus, Jan 7, 2004
    #3
  4. Boefje

    Heiko Fiegen Guest

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


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

    regards,
    heiko
    Heiko Fiegen, Jan 7, 2004
    #4
  5. 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 == 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
    Thomas 'PointedEars' Lahn, Jan 22, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shabam
    Replies:
    2
    Views:
    2,023
    Joe Smith
    Nov 17, 2004
  2. Replies:
    5
    Views:
    454
    Andrea Desole
    Oct 7, 2005
  3. Pete
    Replies:
    1
    Views:
    881
    Martin Honnen
    Nov 27, 2004
  4. Sydex
    Replies:
    12
    Views:
    6,454
    Victor Bazarov
    Feb 17, 2005
  5. ctime double double check

    , Mar 8, 2007, in forum: C Programming
    Replies:
    11
    Views:
    655
    Keith Thompson
    Mar 9, 2007
Loading...

Share This Page