f



test for uniqueness problem

Hi Guys and Gals,

Please can u help me with this little problem.

I am collecting a set  of values from a web form and putting them into a 
JS array object.
I now want to check/ensure that there or no repeated values in this array.  
I can think of many ways to do this using one or many loops but was just 
wondering if there was
any other clever way of doing this using string functions or the RegExp 
object perhaps.

Needless to say I am a JS virgin so any help or advice would be much 
appreciated.

Regards.
Naran Hirani

0
Naran
6/27/2003 1:33:39 PM
comp.lang.javascript 38370 articles. 2 followers. javascript4 (1315) is leader. Post Follow

6 Replies
541 Views

Similar Articles

[PageSpeed] 31

> I am collecting a set  of values from a web form and putting them into a
> JS array object.
> I now want to check/ensure that there or no repeated values in this array.
> I can think of many ways to do this using one or many loops but was just
> wondering if there was
> any other clever way of doing this using string functions or the RegExp
> object perhaps.

Don't use an array, use an object. Use the values as the keys of the object.
Then use for..in to extract the keys.

    var o = {};
    ... some process that produces values ...
        o[value] = true;
    }
    for (value in o) {
        if (value === true) {
            ... value is unique ...
        }
    }

http://www.crockford.com/#javascript


0
Douglas
6/27/2003 1:59:22 PM
Naran Hirani <N.Hirani@hgmp.mrc.ac.uk> writes:

> I am collecting a set  of values from a web form and putting them into
> a JS array object.
> 
> I now want to check/ensure that there or no repeated values in this
> array.  I can think of many ways to do this using one or many loops
> but was just wondering if there was
> 
> any other clever way of doing this using string functions or the
> RegExp object perhaps.

That is sadly a problem that is not solvable by regular expressions [1]. 

> Needless to say I am a JS virgin so any help or advice would be much
> appreciated.

What you can do is to create the elements as properties of an object,
and then check whether you try to create the same property twice.

This function should check an array for duplicate entries (it expects
the entries to be strings, otherwise they are converted to strings
before comparing).

function containsDoubles(array) {
  var object = {};
  for (var i in array) {
    if (object[array[i]]) {
      return true;
    }
    object[array[i]] = true;
  }
  return false;
}

You could also make this check while adding the elements to the array,
but I doubt the change in efficiency is worth it.

/L
[1] Perl's regular expressions might work, since they extend their
RegExps with features that are not regular, but it will not be efficient.
-- 
Lasse Reichstein Nielsen  -  lrn@hotpop.com
 Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
  'Faith without judgement merely degrades the spirit divine.'
0
Lasse
6/27/2003 2:09:48 PM
Thanks Douglas, and Lasse for your replies and code fragments.
I was all set to tackle this problem using arrays, but after your 
warning and suggestions
I will implement something along the lines you guys are suggesting.

Regards,
Naran.


Douglas Crockford wrote:

>>I am collecting a set  of values from a web form and putting them into a
>>JS array object.
>>I now want to check/ensure that there or no repeated values in this array.
>>I can think of many ways to do this using one or many loops but was just
>>wondering if there was
>>any other clever way of doing this using string functions or the RegExp
>>object perhaps.
>>    
>>
>
>Don't use an array, use an object. Use the values as the keys of the object.
>Then use for..in to extract the keys.
>
>    var o = {};
>    ... some process that produces values ...
>        o[value] = true;
>    }
>    for (value in o) {
>        if (value === true) {
>            ... value is unique ...
>        }
>    }
>
>http://www.crockford.com/#javascript
>
>
>  
>

0
Naran
6/27/2003 2:19:11 PM
"Lasse Reichstein Nielsen" <lrn@hotpop.com> wrote in message
news:d6gz393n.fsf@hotpop.com...
<snip>
> function containsDoubles(array) {
>   var object = {};
>   for (var i in array) {
>     if (object[array[i]]) {
>       return true;
>     }
>     object[array[i]] = true;
>   }
>   return false;
> }

I was recently bitten while trying this. Somehow the string
'constructor' found its way into the data and of course
object.constructor is always true. I ended up having to explicitly
prefix all of the data strings with 'Q_' to safely be able to use them
as Object property names without risking conflicts with existing
JavaScript Object properties.

Richard.


0
Richard
6/27/2003 3:05:28 PM
"Richard Cornford" <Richard@litotes.demon.co.uk> writes:

> I was recently bitten while trying this. Somehow the string
> 'constructor' found its way into the data and of course
> object.constructor is always true. I ended up having to explicitly
> prefix all of the data strings with 'Q_' to safely be able to use them
> as Object property names without risking conflicts with existing
> JavaScript Object properties.

Good point! The apparently empty object ... isn't.
You could also prefix with " " (space), but "Q_" is probably safe enough.

/L
-- 
Lasse Reichstein Nielsen  -  lrn@hotpop.com
 Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
  'Faith without judgement merely degrades the spirit divine.'
0
Lasse
6/27/2003 3:47:34 PM
"Lasse Reichstein Nielsen" <lrn@hotpop.com> wrote in message
news:u1ab1q09.fsf@hotpop.com...
<snip>
>You could also prefix with " " (space), but "Q_" is
>probably safe enough.

That is a good suggestion, I was thinking in terms of legal JavaScript
identifiers and looking for an identifier prefix that would be
improbable. A space character is not only improbable but actually
impossible in dot notation, and in the context of a square bracket
reference its legality as an identifier is not significant.

It might make the code less easy to comprehend, being superficially
mistakable for - ""+stringVar; - as a method to force type conversion to
a string, but an appropriate comment should sort that out.

Richard.


0
Richard
6/27/2003 11:20:44 PM
Reply: