Jump to content

Finding First Duplicate in Array


waslsdnowlds

Recommended Posts

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 3
1 does not equal 5
1 does not equal 5
1 does not equal 7
1 does not equal 8
2 does not equal 3
2 does not equal 5
2 does not equal 5
2 does not equal 7
2 does not equal 8
3 does not equal 5
3 does not equal 5
3 does not equal 7
3 does not equal 8
5 does not equal 7
5 does not equal 8
5 does not equal 7
5 does not equal 8

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites


<?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.';
}
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.