Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Making array elements Unique

Reply
Thread Tools

Making array elements Unique

 
 
George Kinley
Guest
Posts: n/a
 
      05-11-2004
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
--


 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      05-11-2004
George Kinley <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      05-11-2004
"George Kinley" <(E-Mail Removed)> 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\\
 
Reply With Quote
 
John Bokma
Guest
Posts: n/a
 
      05-11-2004
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/
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      05-11-2004
Bernard El-Hagin wrote:
> "George Kinley" <(E-Mail Removed)> 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


 
Reply With Quote
 
ctcgag@hotmail.com
Guest
Posts: n/a
 
      05-11-2004
"George Kinley" <(E-Mail Removed)> 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
 
Reply With Quote
 
Kevin Collins
Guest
Posts: n/a
 
      05-11-2004
In article <n65oc.113943$(E-Mail Removed)>, Jürgen Exner wrote:
> Bernard El-Hagin wrote:
>> "George Kinley" <(E-Mail Removed)> 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
 
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
Making a count of unique elements in an array Dan Kohn Ruby 6 12-28-2013 11:02 AM
list question... unique values in all possible unique spots ToshiBoy Python 6 08-12-2008 05:01 AM
Filter unique elements in an array algorithm ck Java 17 03-09-2007 02:27 AM
How to get non-unique elements from an array? Sam Kong Ruby 64 10-19-2005 03:58 AM
unique elements (case insentitive) of an array in perl jjl Perl 2 10-20-2003 06:23 AM



Advertisments