March 20, 2012

Determining duplicate letter counts between an array of strings

Question by kavisiegel

I have an array of strings of random letters, and I need to know which letters are consistent between the array members. The count of the letters are important.

My method right now is loop through the array, doing a split, then looping through the spitted string to count the occurrences of each letter, then update the array with letter => count

Then do an array_reduce that creates a new array of members who only occur in all arrays. But, it’s not working.

<?
$a[] = "emaijuqqrauw";
$a[] = "aaeggimqruuz";
$a[] = "aabimqrtuuzw";
$a[] = "aacikmqruuxz";
$a[] = "aacikmqruuxz";
$a[] = "aaciimqruuxy";

foreach($a as $b){
    $n = str_split($b, 1);
    foreach($n as $z){
        $arr[$z] = substr_count($b, $z);
    }
    ksort($arr);
    $array[] = $arr;
    unset($arr);
}

$n = array_reduce($array, function($result, $item){
    if($result === null){
        return $item;
    }else{
        foreach($item as $key => $val){
            if(isset($result[$key])){
                $new[$key] = $val;
            }
        }
        return $new;
    }
});

foreach($n as $key => $val){
    echo str_repeat($key, $val);
}

This returns aaiimqruu – which is kinda right, but there’s only 2 i‘s in the last element of the array. There’s only one i in the rest. I’m not sure how to break that down farther and get it to return aaimqruu– which I’ll then pop into a SQL query to find a matching word, aquarium

Answer by kavisiegel

Seems like array_reduce is the best function for what this purpose, however I just didn’t think of adding a conditional to give me the desired effect.

$new[$key] = ($result[$key] > $val) ? $val : $result[$key];

to replace

$new[$key] = $val;

did the trick.

Answer by Starx

How about you do it this way? Finds out the occurrence of an item throughout the array.

function findDuplicate($string, $array) {
   $count = 0;
   foreach($array as $item) {
        $pieces = str_split($item);
        $pcount= array_count_values($pieces);
        if(isset($pcount[$string])) {
           $count += $pcount[$string];
        }
   }
   return $count;
}

echo findDuplicate("a",$a);

Tested 🙂

Gives 12, using your array, which is correct.


Update

My solution above already had your answer

    $pieces = str_split($item);
    $pcount= array_count_values($pieces);
    //$pcount contains, every count like [a] => 2

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!