Check for double occurrences

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Boefje

    #1

    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.
  • Brian Genisio

    #2
    Re: Check for double occurrences

    Boefje wrote:
    [color=blue]
    > 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.[/color]

    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



    Comment

    • Disco Octopus

      #3
      Re: Check for double occurrences

      in news:287dc9c0.0 401070523.91169 66@posting.goog le.com,
      Boefje typed:[color=blue]
      > 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.[/color]

      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 ("howaboutbe ans = document.forms[0].thingy" + i + ".value");
      if (telephonenumbe r[howaboutbeans] == howaboutbeans) {
      alert ("Found duplicate " + howaboutbeans);
      }
      telephonenumber[howaboutbeans] = howaboutbeans;
      }
      }
      </script>
      <body>

      <form name="freakenfr ackitty">
      <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="eatyou rpeas();"><br>

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








      Comment

      • Heiko Fiegen

        #4
        Re: Check for double occurrences

        Disco Octopus wrote ...[color=blue]
        >in news:287dc9c0.0 401070523.91169 66@posting.goog le.com,
        >eval("howabout beans = document.forms[0].thingy" + i + ".value");[/color]

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

        regards,
        heiko



        Comment

        • Thomas 'PointedEars' Lahn

          #5
          Re: Check for double occurrences

          Brian Genisio wrote:
          [color=blue]
          > 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...[/color]

          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_evalu ated_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

          Comment

          Working...