Best Way to Check Uniqueness in Array

Discussion in 'Perl Misc' started by Kasp, Nov 13, 2003.

  1. Kasp

    Kasp Guest

    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.
    --
    Kasp, Nov 13, 2003
    #1
    1. Advertising

  2. 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
    Jürgen Exner, Nov 13, 2003
    #2
    1. Advertising

  3. In article <bp06v3$1ii9gm$-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
    Andreas Kahari, Nov 13, 2003
    #3
  4. Kasp

    Anno Siegel Guest

    Andreas Kahari <> wrote in comp.lang.perl.misc:
    > In article <bp06v3$1ii9gm$-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
    Anno Siegel, Nov 13, 2003
    #4
  5. Kasp

    Kasp Guest

    Kasp, Nov 13, 2003
    #5
  6. In article <bp0886$2sl$-Berlin.DE>, Anno Siegel wrote:
    > Andreas Kahari <> 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
    Andreas Kahari, Nov 13, 2003
    #6
    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. Ognen Ivanovski
    Replies:
    0
    Views:
    939
    Ognen Ivanovski
    Jul 15, 2003
  2. Replies:
    78
    Views:
    1,340
    Mike Schilling
    Feb 21, 2007
  3. Adam Gardner
    Replies:
    5
    Views:
    141
    Sebastian Hungerecker
    Nov 19, 2008
  4. James Byrne
    Replies:
    2
    Views:
    83
    James Byrne
    Feb 4, 2011
  5. James Byrne
    Replies:
    3
    Views:
    76
    James Byrne
    Feb 7, 2011
Loading...

Share This Page