finding duplicates in an array
Posted: Wed Mar 02, 2005 11:06 pm
i have a list of about 11000 companies and the state they belong to in a txt file. there are about 300 duplicate company names. i want to find the duplicates names and print them out. (regardless of what state shows next to the name).
the important thing is i need to know which ones have duplicates.
here is what i have now. this is going to run forever. there has got to be a better way.
the important thing is i need to know which ones have duplicates.
here is what i have now. this is going to run forever. there has got to be a better way.
Code: Select all
<?php
$file = file("companies.txt");
$num = count($file);
$i = 0;
print "<b>$num</b><br>";
while($i < $num) {
$line = chop($fileї$i]);
$line = split("\t", $line);
$companiesї] = $lineї0];
$statesї] = $lineї1];
$i++;
}
$new_array = $companies;
$num = count($companies);
$count = 0;
$i = 0;
$x = 0;
while($i < $num) {
while($x < count($companies)) {
if ($new_arrayї$i] == $companiesї$x]) {
$count++;
if($count >= 2) {
print "$new_arrayї$i] $statesї$i]<br>";
}
$x++;
}
$x = 0;
$i++;
}
?>