Velocity Reviews > Perl > Best Way to Check Uniqueness in Array

# 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.
--

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

jue

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

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

Kasp
Guest
Posts: n/a

 11-13-2003
> This has been discussed a few times before, please see "perldoc -q

Sorry, didn't do my homework
--

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

 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 OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post James Byrne Ruby 3 02-07-2011 02:16 PM James Byrne Ruby 2 02-04-2011 04:42 PM Adam Gardner Ruby 5 11-19-2008 07:36 AM krislioe@gmail.com Java 78 02-21-2007 08:18 PM Ognen Ivanovski XML 0 07-15-2003 02:36 PM