Skip to content

Instantly share code, notes, and snippets.

@connordavison
Last active October 9, 2015 08:52
Show Gist options
  • Save connordavison/ffb827da0fb68a524aee to your computer and use it in GitHub Desktop.
Save connordavison/ffb827da0fb68a524aee to your computer and use it in GitHub Desktop.
Functions for identifying duplicates within arrays.
<?php
/**
* Count duplicates in an array before and after each element.
*
* <code>
* $counts = count_duplicates_both_ways([0, 1, 1, 2, 2, 1]);
* print_r($counts);
* // [
* // 'before' => [0, 0, 1, 0, 1, 2],
* // 'after' => [0, 2, 1, 1, 0, 0]
* // ]
* </code>
*/
function count_duplicates_both_ways(array $arr)
{
// Store results of duplicate checks
$results_before = $results_after = array();
// Count occurrences of elements before and after current element
$counter_before = $counter_after = array();
// Initialise counters. By default, $counter_before should be full of
// zeroes, and $counter_after should contain the number of occurences of
// each element.
foreach ($arr as $v) {
if (!isset($counter_before[$v])) {
$counter_before[$v] = 0;
}
if (!isset($counter_after[$v])) {
$counter_after[$v] = 0;
}
$counter_before[$v] = 0;
$counter_after[$v]++;
}
// Calculate before/after duplicates
foreach ($arr as $v) {
// Remove the current element from $counter_after
$counter_after[$v]--;
// If the counters for this element are > 0, then that indicates that
// there exists an element equal to this one before/after this one in
// $arr.
$results_before[] = $counter_before[$v] > 0;
$results_after[] = $counter_after[$v] > 0;
// Add this element to $counter_before
$counter_before[$v]++;
}
return array('before' => $results_before, 'after' => $results_after);
}
<?php
/**
* @param array $arr The array to check for duplicates
* @return bool True if the array contains any duplicates
*/
function has_duplicate(array $arr)
{
$head = array();
foreach ($arr as $e) {
if (in_array($e, $head)) return true;
$head[] = $e;
}
}
/**
* @param array $arr The array to check for duplicates
* @return bool True if the array contains any duplicates
*/
function has_duplicates(array $arr)
{
for ($i = 0; $i < count($arr) ; $i++) {
for ($j = 0; $j < $i; $j++) {
if ($arr[$j] === $arr[$i]) return true;
}
}
}
/**
* Determine which elements of an array have backward duplicates.
*
* <code>
* $arr = [0,0,1,1,2,3,4,3,2,1];
* $dupes = has_backward_duplicates($arr);
*
* print_r($dupes); // [0, 1, 0, 1, 0, 0, 0, 1, 1, 1]
* print_r($arr); // [0, 0, 1, 1, 2, 3, 4, 3, 2, 1]
* </code>
*
* @param array $arr The array to check for duplicates
* @return array An array of booleans, same length as $arr. Values are true
* where $arr contains backwards duplicates
*/
function has_backward_duplicates(array $arr)
{
$results = array();
for ($i = 0; $i < count($arr) ; $i++, $duped = false) {
// Loop backward through previous elements to look for dupes
for ($j = 0; $j < $i; $j++) {
if ($arr[$j] === $arr[$i]) $duped = true;
}
$results[] = $duped;
}
return $results;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment