Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-12-2010, 05:23 PM   PM User | #1
grumpy
Regular Coder

 
grumpy's Avatar
 
Join Date: Oct 2006
Location: Visible light spectrum
Posts: 120
Thanks: 5
Thanked 6 Times in 6 Posts
grumpy is an unknown quantity at this point
Exclamation Pinpoint duplicates in array and change them

I have a one dimensional array of integers (single and double digit full numbers).

I would like to detect the presence of duplicate numbers, then identify all the elements of the array that are duplicates, return and extract them and change these duplicate numbers one by one to different numbers, based on my rules, re-insert the new numbers into the original array (that now has only unique numbers left in it) and keep checking for duplicates and changing the numbers until all array elements are unique.

I have tried array_intersect, but that only returns the values of the duplicates not all the elements that are duplicates and tried playing around with array_count_values but that didn't produce either.

Thanks for badly needed insights...

Thank you very much

Bo

Last edited by grumpy; 02-12-2010 at 05:26 PM..
grumpy is offline   Reply With Quote
Old 02-12-2010, 05:56 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
To first remove the duplicates, use the array_unique() function. This will return a new array minus the duplicates.
Next, find you're duplicates by using array_diff(). The order of arrays is important, you will need to provide the original array as the first argument, and the unique array as the second. That should return an array with all of the duplicate values, but its stability is questionable (as in, I don't know which specific elements it will choose for the duplicates - it could be the first, or last of any duplicate group). This is unlikely to matter unless you're tracking with keys that have some type of importance.

The solution I would actually take is to not allow them in the first place. Arrays in PHP are not controllable, but you can use an array object to you're advantage (5.2+).
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
grumpy (02-12-2010)
Old 02-12-2010, 06:17 PM   PM User | #3
grumpy
Regular Coder

 
grumpy's Avatar
 
Join Date: Oct 2006
Location: Visible light spectrum
Posts: 120
Thanks: 5
Thanked 6 Times in 6 Posts
grumpy is an unknown quantity at this point
Quote:
Originally Posted by Fou-Lu View Post
The solution I would actually take is to not allow them in the first place. Arrays in PHP are not controllable, but you can use an array object to you're advantage (5.2+).
You are 100% right. I think I better validate the numbers when they are created to each other instead of removing duplicates at the end. Much easier and more full-proof. Wonderful. Thank you for the great idea!
grumpy is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 07:53 PM.


Advertisement
Log in to turn off these ads.