Making array elements Unique

Discussion in 'Perl Misc' started by George Kinley, May 11, 2004.

  1. Hi

    I have an array of duplicate elements "char", I want to make the elements
    Unique and throw the redundant
    what solution came to my mind is I take element by element out of an array
    and create a hash checking the elements with "exists" and populate the hash
    is there any more intelligent way to do
    --
    George Kinley, May 11, 2004
    #1
    1. Advertising

  2. George Kinley

    Anno Siegel Guest

    George Kinley <> wrote in comp.lang.perl.misc:
    > Hi
    >
    > I have an array of duplicate elements "char", I want to make the elements
    > Unique and throw the redundant
    > what solution came to my mind is I take element by element out of an array
    > and create a hash checking the elements with "exists" and populate the hash
    > is there any more intelligent way to do


    It's the standard way. See "perldoc -q duplicate".

    Anno
    Anno Siegel, May 11, 2004
    #2
    1. Advertising

  3. "George Kinley" <> writes:

    > I have an array of duplicate elements "char", I want to make the elements
    > Unique and throw the redundant
    > what solution came to my mind is I take element by element out of an array
    > and create a hash checking the elements with "exists" and populate the hash
    > is there any more intelligent way to do


    If there were then it is unlikely that the solution you describe above
    would be the one given in the FAQ. It is. Well almost. For details,
    see FAQ.

    --
    \\ ( )
    . _\\__[oo
    .__/ \\ /\@
    . l___\\
    # ll l\\
    ###LL LL\\
    Brian McCauley, May 11, 2004
    #3
  4. George Kinley

    John Bokma Guest

    George Kinley wrote:

    > Hi
    >
    > I have an array of duplicate elements "char", I want to make the elements
    > Unique and throw the redundant
    > what solution came to my mind is I take element by element out of an array
    > and create a hash checking the elements with "exists" and populate the hash
    > is there any more intelligent way to do


    Since you want to check each element, which is O(n), no.

    --
    John MexIT: http://johnbokma.com/mexit/
    personal page: http://johnbokma.com/
    Experienced Perl programmer available: http://castleamber.com/
    John Bokma, May 11, 2004
    #4
  5. Bernard El-Hagin wrote:
    > "George Kinley" <> wrote:
    >> I have an array of duplicate elements "char", I want to make the
    >> elements Unique and throw the redundant
    >> what solution came to my mind is I take element by element out of
    >> an array and create a hash checking the elements with "exists" and
    >> populate the hash is there any more intelligent way to do

    >
    >
    > No.


    Actually there is. With a hash there is no need to check for exists()
    because even adding the same key multiple times will result in only one
    entry by the very nature of hashes. No need to waste time with exists().

    jue
    Jürgen Exner, May 11, 2004
    #5
  6. George Kinley

    Guest

    "George Kinley" <> wrote:
    > Hi
    >
    > I have an array of duplicate elements "char", I want to make the elements
    > Unique and throw the redundant
    > what solution came to my mind is I take element by element out of an
    > array and create a hash checking the elements with "exists" and populate
    > the hash is there any more intelligent way to do


    Yes. Use a hash slice to populate the hash in one fell swoop.
    Or better yet, don't make an array in the first place and use the hash
    throughout.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
    , May 11, 2004
    #6
  7. In article <n65oc.113943$>, Jürgen Exner wrote:
    > Bernard El-Hagin wrote:
    >> "George Kinley" <> wrote:
    >>> I have an array of duplicate elements "char", I want to make the
    >>> elements Unique and throw the redundant
    >>> what solution came to my mind is I take element by element out of
    >>> an array and create a hash checking the elements with "exists" and
    >>> populate the hash is there any more intelligent way to do

    >>
    >>
    >> No.

    >
    > Actually there is. With a hash there is no need to check for exists()
    > because even adding the same key multiple times will result in only one
    > entry by the very nature of hashes. No need to waste time with exists().
    >


    Yes, however you do not end up with the same sort order. Of course, the OP did
    not mention that, but it can be a concern.

    Kevin
    Kevin Collins, May 11, 2004
    #7
    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. jjl
    Replies:
    2
    Views:
    4,275
    Andrew Shitov
    Oct 20, 2003
  2. ck
    Replies:
    17
    Views:
    3,612
    David Segall
    Mar 9, 2007
  3. ToshiBoy
    Replies:
    6
    Views:
    831
    ToshiBoy
    Aug 12, 2008
  4. Sam Kong
    Replies:
    64
    Views:
    646
  5. Dan Kohn
    Replies:
    6
    Views:
    288
    Robert Klemme
    Dec 28, 2013
Loading...

Share This Page