Majic Johnson
Majic Johnson

Reputation: 341

Most efficient way to count the number of duplicate elements in PHP

Imagine we have an array of students:

Students: Ben Sam ...

Every student has a group of books at home:

Books:

So in PHP, the $students data structure looks like this:

Array
(
    [0] => Array
        (
            [name] => Joe Smith
            [books] => Array
                (
                    [0] => A
                    [1] => B
                    [2] => C
                )

        )

)

We want to count how many times A has been repeated and who owns A B and C etc. One way to do it in php is to do this:

if (sizeof($potential_entries) > 0) :
  for ($j = 0, $potentialsize = sizeof($potential_entries); $j < $potentialsize; ++$j)     {
    for ($k = 0, $listsize = sizeof($student_book); $k < $listsize; ++$k) {
      if ($student_book[$k] == $potential_entry[$k]) :
        $match = 1;
        $student_book[$k]['like_count']++;
       endif;
    }
  }

This is not very efficient we rather want a map structure or a hashtable, Does php has those structures like perl? or do we have to manually build them. Edit: I can see in the PHP that we have associative array? Can you please give an example of that could not find it in the documentation. for sam:

var_dump($potential_entry):

[0]=>array(2) { ["name"]=> string(1) "A" ["id"]=> string(11) "1348"}
[1]=>array(2) { ["name"]=> string(1) "B" ["id"]=> string(11) "1483"}
[2]=>array(2) { ["name"]=> string(1) "C" ["id"]=> string(11) "13"}
[3]=>array(2) { ["name"]=> string(1) "D" ["id"]=> string(11) "1174"}

That is for Sam, for the rest we have the same structure. so sam is array and books is an array and sam has an array of books. In this example

Upvotes: 1

Views: 400

Answers (2)

Anton
Anton

Reputation: 1051

$aFullCount = array(); // in this array you will have results of all characters count.
$aCondition = array("A", "B"); // set condition you need to count down here.
$aConditionMatch = array(); // in here you will have all users ids who matched your A + B
if ($aUsers){
    foreach ($aUsers as $sUserKey=>$aOneUser){
        $aForCondition = array();
        if ($aPotentialEntries){
            foreach ($aPotentialEntries as $aOnePotentialEntry){
                $aForCondition[] = $aOnePotentialEntry["name"];
                if (!isset($aFullCount[$aOnePotentialEntry["name"]]))
                    $aFullCount[$aOnePotentialEntry["name"]] = 1;
                else
                    $aFullCount[$aOnePotentialEntry["name"]]++;
            }
        }

        $aConditionCheck = array_intersect($aForCondition, $aCondition);
        if (!array_diff($aConditionCheck, $aCondition))
            $aConditionMatch[] = $sUserKey;
    }
}

Upvotes: 1

Scott C Wilson
Scott C Wilson

Reputation: 20026

If you have $students as an array, and each entry in this array has an array of books which is called books

$books = array(); 
for ($i = 0, $count = sizeof($students); $i++) {
   foreach ($student[$i]['books'] as $book) { 
      if (isset($books[$book])) { 
          $books[$book]++; 
      } else { 
          $books[$book] = 1;
      }
   }
}

Then after this, any $books entry whose count is > 1 is a duplicate, i.e.

foreach ($books as $key => $value) { 
   if ($value) > 1) { 
        echo "Book " . $key . " is a duplicate (" . $value . ")"; 
   }
}

Upvotes: 1

Related Questions