Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder grumpy's Avatar
    Join Date
    Oct 2006
    Location
    Visible light spectrum
    Posts
    121
    Thanks
    5
    Thanked 6 Times in 6 Posts

    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.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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+).
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    grumpy (02-12-2010)

  • #3
    Regular Coder grumpy's Avatar
    Join Date
    Oct 2006
    Location
    Visible light spectrum
    Posts
    121
    Thanks
    5
    Thanked 6 Times in 6 Posts
    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!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •