Making array elements Unique

G

George Kinley

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
--
 
A

Anno Siegel

George Kinley said:
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
 
B

Brian McCauley

George Kinley said:
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\\
 
J

John Bokma

George said:
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.
 
J

Jürgen Exner

Bernard said:

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
 
C

ctcgag

George Kinley said:
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
 
K

Kevin Collins

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top