2

I am trying to find all duplicates in my array, and create a new array which has keys as the duplicate values key and value as the key of its duplicate

example

[1] => 10
[2] => 11
[3] => 12
[4] => 12
[5] => 12
[6] => 13
[7] => 13

After I apply the duplicate check I just need

[4] => [3] // value of key 4 is dupe of key 3
[5] => [3] // value of key 5 is dupe of key 3
[7] => [6] // value of key 7 is dupe of key 6

this gets me all duplicate keys, but I need duplicate keys with values as the keys which are duplicate

$arr_duplicates = array_keys(array_unique( array_diff_assoc( $array, array_unique( $array ) ) ));

Thanks

6
  • Wha? I need one million things that are a thing? Commented Jul 29, 2013 at 17:16
  • @Neal sorry its hard to explain what I am trying to explain Basically I just need to get all dupes as an array but the values of my new array should be the key from which they are dupes of. Commented Jul 29, 2013 at 17:18
  • 2
    Why did you edit the output format? You can't have two elements with the same key (3). Commented Jul 29, 2013 at 17:21
  • @RocketHazmat - I have added few comments to explain what I need in more details Commented Jul 29, 2013 at 17:22
  • @user2619159: You still can't have key 3 equal to both 4 and 5 (unless you make it an array). Commented Jul 29, 2013 at 17:23

2 Answers 2

2

Try this for potential speed boost over the other solution. Will however use a lot more memory on large data sets.

<?php

$orig = array(
    1   => 10,
    2   => 11,
    3   => 12,
    4   => 12,
    5   => 12,
    6   => 13,
    7   => 13
);

$seen  = array();
$dupes = array();

foreach ($orig as $k => $v) {
    if (isset($seen[$v])) {
        $dupes[$k] = $seen[$v];
    } else {
        $seen[$v] = $k;
    }
}
unset($seen);

var_dump($dupes);
Sign up to request clarification or add additional context in comments.

1 Comment

Nice one, thats very fast
1

This should do what you want. Loop over the array, and see if the value was already in there. If so, add it to the result.

$arr_duplicates = array();
foreach($array as $k=>$v){
    // array_search returns the 1st location of the element
    $first_index = array_search($v, $array);
    // if our current index is past the "original" index, then it's a dupe
    if($k != $first_index){
        $arr_duplicates[$k] = $first_index;
    }
}

DEMO: http://ideone.com/Kj0dUV

6 Comments

Change $k > $first_index to $k != $first_index to make it work with indexes in any order, such as $array = [7 => 13, 6 => 13].
@RocketHazmat this is a great function, I had tried foreach() before too, the only issue ive had is, am working with very large arrays i.e 10000++ records, and speed is very important
@user2619159: Anything else you try is probably gonna be slower. Do you notice a speed issue with this code? If not, then it's probably not an issue :-P
Speed can be improved by keeping track of the first time each value was seen. You'd be trading a possible speed boost for an enormous amount of memory usage.
@Nate: I guess you could cache the array_search lookups, that may be faster, but I'm not sure how much.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.