waslsdnowlds Posted September 28, 2017 Share Posted September 28, 2017 I'm trying to create some code that finds the first duplicate of an array and echo's it out. I've been working about two hours on this bad boy and have got to this point. Code and results are below. Any suggestions on why the 5 skips the next 5? $array = array(1,2,3,5,5,7,; for($current_index = 0; $current_index < count($array); $current_index++){ $current_value = $array[$current_index]; $next_index = $current_value; for($comparing_index = $next_index; $comparing_index < count($array); $comparing_index++){ $comparing_value = $array[$comparing_index]; if ($current_value != $comparing_value){ echo "$current_value does not equal $comparing_value<br>"; } else { echo "$current_value equals $comparing_value"; break; } } } Results 1 does not equal 2 1 does not equal 31 does not equal 51 does not equal 51 does not equal 71 does not equal 82 does not equal 32 does not equal 52 does not equal 52 does not equal 72 does not equal 83 does not equal 53 does not equal 53 does not equal 73 does not equal 85 does not equal 75 does not equal 85 does not equal 75 does not equal 8 Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted September 29, 2017 Share Posted September 29, 2017 a) I would use array_count_values() to get a count of each value in the starting array. remove all entries with count of one, leaving only entries with count > 1. then use array_keys() (which are the values having a count > 1) to get an array with the values. loop over the starting array, using a simple foreach(){} loop. if the value in the foreach loop is in the array (see - in_array()) produced in part a), you have found the first duplicate. use break; to exit the loop. Quote Link to comment Share on other sites More sharing options...
waslsdnowlds Posted September 29, 2017 Author Share Posted September 29, 2017 (edited) Thanks for the suggestions. While I was waiting for someone to reply I found this to give me the first duplicate. Any suggestions on making the code smaller? $array = array(1,2,3,5,5,7,8,; for($current_index = 0; $current_index < count($array)-1; $current_index++){ $current_value = $array[$current_index]; $next_index = $current_index+1; for($comparing_index = $next_index; $comparing_index < count($array)-1; $comparing_index++){ $comparing_value = $array[$comparing_index]; if ($current_value != $comparing_value){ } else { echo "First Duplicate: $current_value"; return; } } } Results First Duplicate: 5 Edited September 29, 2017 by waslsdnowlds Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted September 29, 2017 Share Posted September 29, 2017 <?php $array = array(1,2,3,5,5,7,; // find the count of values $counts = array_count_values($array); // keep values > 1 and get the keys, which are the values. if you want ONLY duplicates, remove all values except 2's. $counts = array_keys(array_filter($counts, function($k) { return $k > 1; })); $found = false; // loop until the first value found that's in the $counts array. foreach($array as $value) { if(in_array($value,$counts)) { $found = true; break; } } // display the results if($found) { echo "The first duplicate found was - $value"; } else { echo 'There were no duplicates in the data.'; } Quote Link to comment Share on other sites More sharing options...
benanamen Posted September 29, 2017 Share Posted September 29, 2017 Not sure if you really meant ONLY the first duplicate. If not then... <?php $arr = array(1,2,3,5,5,7,; foreach(array_count_values($arr) as $k => $v) if($v > 1) $dups[] = $k; print_r($dups); Quote Link to comment Share on other sites More sharing options...
requinix Posted September 29, 2017 Share Posted September 29, 2017 PS: the original bug was $next_index = $current_value;mixing values with indexes. 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.