User Control Panel
Advertisements

HELP US, HELP YOU!

Removing array duplicates

 
Post new topic   Reply to topic    Bot Depot Forum Index -> Code Help
View unanswered posts
Author Message
Teario
Member
Member


Joined: 25 Jun 2004
Posts: 130
Location: Liverpool(home) or Derby(uni), UK
Reputation: 59.8
votes: 3

PostPosted: Tue Oct 17, 2006 12:50 pm    Post subject: Removing array duplicates Reply with quote

I have made this little program which works with myspace. You give it a start and end profile id, and it will use the top friends list to find a path from the start profile to the end. I have an array to store the IDs that still have to be processed, and each time one is processed, the array expands by a multiple of 4 between 4 and 28. When I get the ids though, I dont want to add duplicates to the list of ids still to process. I was wondering if someone could give me the best way to remove duplicates from a very large array, whilst still preserving the order in which the values were pushed into it?
Back to top
Cer
Upgraded Agent
Upgraded Agent


Joined: 03 Feb 2004
Posts: 3776
Location: Michigan
Reputation: 146.9
votes: 4

PostPosted: Tue Oct 17, 2006 4:08 pm    Post subject: Reply with quote

Try something like:

Code:
my %unique = ();
map { $unique{$_}++ } @array;

# then if $unique{"friend id"} exists,
# it's already in the array somewhere

# you could add it to the array then this way:
$unique{"new friend id"}++;

# so if it already existed, no big deal, just
# $unique{"new friend id"} might equal 2 or 3
# rather than 1

# and you can repopulate the array from the
# hash keys
@array = ( keys %unique );

# so each friend id is only in @array once now


See if that helps.

_________________
Current Site (2008) http://www.cuvou.com/
Back to top
mattaustin
Sentinel
Sentinel


Joined: 19 Jul 2004
Posts: 556
Location: Los Angeles, CA
Reputation: 50.7
votes: 1

PostPosted: Tue Oct 17, 2006 6:30 pm    Post subject: Reply with quote

He said he wanted to keep the order of the array (because he is looking at the top list), but keys of a hash will not be in the same order that you added them.


This will use a hash just to check if we have used it yet so we maintain the order.
Code:

@a = (8,1,1,3,5,6,8,9,9);
my @a = grep !$s{$_}++, @a;
print @a;


you could then use the $s hash to know how many time an element was used:

Code:

print $s{8};

_________________
[ matt ]
Back to top
Teario
Member
Member


Joined: 25 Jun 2004
Posts: 130
Location: Liverpool(home) or Derby(uni), UK
Reputation: 59.8
votes: 3

PostPosted: Fri Oct 20, 2006 3:13 pm    Post subject: Reply with quote

Thanks, I used matts code and it works perfectly.

3 Days of running the program and still no results yet X_x
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Bot Depot Forum Index -> Code Help All times are GMT
Page 1 of 1

 



Protected by phpBB Security phpBB-TweakS
phpBB Security Has Blocked 9 Exploit Attempts.
Antispam Captcha Mod by phpbb-security.com
Powered by phpBB © 2001, 2005 phpBB Group