Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Best Way to Check Uniqueness in Array

Reply
Thread Tools

Best Way to Check Uniqueness in Array

 
 
Kasp
Guest
Posts: n/a
 
      11-13-2003
Hello,

Consider an unsorted array
my @array = ( 2, 4, 6, 4, 2, 6, A, B, C, C, B, A);

Now, I wish to remove the duplicate elements from this array. A good way may
be to assign these array's elements as keys of a hash. And then convert the
keys back to the array like this:
<UNTEST CODE>

my @array = ( 2, 4, 6, 4, 2, 6, A, B, C, C, B, A);
my %hash = ();
for($i=0;$i<=$#array;$i++){
$hash{ $array[$i] } = '';
}

(@array) = (keys %hash); # @array now contains unique elements
</UNTEST CODE>

Is there a better or shorter way?

Thanks.
--



 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      11-13-2003
Kasp wrote:
> Consider an unsorted array
> my @array = ( 2, 4, 6, 4, 2, 6, A, B, C, C, B, A);
>
> Now, I wish to remove the duplicate elements from this array.


This has been discussed a few times before, please see "perldoc -q
duplicate" or http://www.badtz.org/pics/bart.gif

jue


 
Reply With Quote
 
 
 
 
Andreas Kahari
Guest
Posts: n/a
 
      11-13-2003
In article <bp06v3$1ii9gm$(E-Mail Removed)-berlin.de>, Kasp wrote:
> Hello,
>
> Consider an unsorted array
> my @array = ( 2, 4, 6, 4, 2, 6, A, B, C, C, B, A);
>
> Now, I wish to remove the duplicate elements from this array. A good way may
> be to assign these array's elements as keys of a hash. And then convert the
> keys back to the array like this:
><UNTEST CODE>

[cut]

my @array = qw( 2 4 6 4 2 6 A B C C B A );
my %hash;

@hash{@array} = 1;
@array = keys %hash;


--
Andreas Kähäri
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      11-13-2003
Andreas Kahari <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> In article <bp06v3$1ii9gm$(E-Mail Removed)-berlin.de>, Kasp wrote:
> > Hello,
> >
> > Consider an unsorted array
> > my @array = ( 2, 4, 6, 4, 2, 6, A, B, C, C, B, A);
> >
> > Now, I wish to remove the duplicate elements from this array. A good way may
> > be to assign these array's elements as keys of a hash. And then convert the
> > keys back to the array like this:
> ><UNTEST CODE>

> [cut]
>
> my @array = qw( 2 4 6 4 2 6 A B C C B A );
> my %hash;
>
> @hash{@array} = 1;


Why assign 1 to the hash slice, that's neither here nor there. Either
leave all values undefined:

@hash{@array} = ();

or set them all to 1:

@hash{@array} = (1) x @array;

or

%hash = map { ( $_, 1) } @array;

> @array = keys %hash;


Mind you, your code works, but assigning 1 to one key and leaving the
others undefined makes no sense.

Anno
 
Reply With Quote
 
Kasp
Guest
Posts: n/a
 
      11-13-2003
> This has been discussed a few times before, please see "perldoc -q
> duplicate" or http://www.badtz.org/pics/bart.gif


Sorry, didn't do my homework
--
PS: Nice link!



 
Reply With Quote
 
Andreas Kahari
Guest
Posts: n/a
 
      11-13-2003
In article <bp0886$2sl$(E-Mail Removed)-Berlin.DE>, Anno Siegel wrote:
> Andreas Kahari <(E-Mail Removed)> wrote in comp.lang.perl.misc:

[cut]
>> @hash{@array} = 1;

>
> Why assign 1 to the hash slice, that's neither here nor there. Either
> leave all values undefined:
>
> @hash{@array} = ();



Yes, that makes more sense.


--
Andreas Kähäri
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Determining uniqueness on a single array element James Byrne Ruby 3 02-07-2011 02:16 PM
problem determining uniqueness in array of arrays James Byrne Ruby 2 02-04-2011 04:42 PM
Subclassing Hash to enforce value uniqueness ala key uniqueness. Adam Gardner Ruby 5 11-19-2008 07:36 AM
How to check variables for uniqueness ? krislioe@gmail.com Java 78 02-21-2007 08:18 PM
XML Schema keys, uniqueness based on ancestor's attribute Ognen Ivanovski XML 0 07-15-2003 02:36 PM



Advertisments