# How do I count occurrence of duplicate items in array

Viewed   113 times

I would like to count the occurrence of each duplicate item in an array and end up with an array of only unique/non duplicate items with their respective occurrences.

Here is my code; BUT I don't where am going wrong!

``````<?php
\$array = array(12,43,66,21,56,43,43,78,78,100,43,43,43,21);

//\$previous[value][Occurrence]

for(\$arr = 0; \$arr < count(\$array); \$arr++){

\$current = \$array[\$arr];
for(\$n = 0; \$n < count(\$previous); \$n++){
if(\$current != \$previous[\$n]){// 12 is not 43 -----> TRUE
if(\$current != \$previous[count(\$previous)]){
\$previous[\$n++] = \$current;
\$previous[\$n++] = \$counter++;
}
}else{
\$previous[\$n] = \$counter++;
unset(\$previous[count(\$previous)-1]);
unset(\$previous[count(\$previous)-1]);
}
}
}
//EXPECTED VALUES
echo 'No. of NON Duplicate Items: '.count(\$previous).'<br><br>';// 7
print_r(\$previous);// array( {12,1} , {21,2} , {43,6} , {66,1} , {56,1} , {78,2} , {100,1})
?>
``````

1

`array_count_values`, enjoy :-)

``````\$array = array(12,43,66,21,56,43,43,78,78,100,43,43,43,21);
\$vals = array_count_values(\$array);
echo 'No. of NON Duplicate Items: '.count(\$vals).'<br><br>';
print_r(\$vals);
``````

Result:

``````No. of NON Duplicate Items: 7
Array
(
 => 1
 => 6
 => 1
 => 2
 => 1
 => 2
 => 1
)
``````
Thursday, September 1, 2022

5

You are assigning `\$count` to 0 in your conditional statement

``````if (\$count = 0)
``````

Do this

``````if (\$count === 0)
``````
Saturday, November 19, 2022

3

You can do this:

``````echo count(\$food['fruits']);
echo count(\$food['veggie']);
``````

If you want a more general solution, you can use a foreach loop:

``````foreach (\$food as \$type => \$list) {
echo \$type." has ".count(\$list). " elementsn";
}
``````
Wednesday, December 14, 2022

1

You could use a `MultiSet` from Google Collections/Guava or a `Bag` from Apache Commons.

If you have a collection instead of an array, you can use `addAll()` to add the entire contents to the above data structure, and then apply the `count()` method to each value. A `SortedMultiSet` or `SortedBag` would give you the items in a defined order.

Google Collections actually has very convenient ways of going from arrays to a `SortedMultiset`.

Sunday, October 2, 2022

1

You could use the bitwise AND in python and compare them by converting the list of lists to a list of sets

``````>>> set(['cat','dog']) & set(['cat','dog','monkey','horse','fish'])
set(['dog', 'cat'])
``````

You could use this property and achieve the count you've wanted.

``````def listOccurences(item, names):
# item is the list that you want to check, eg. ['cat','fish']
# names contain the list of list you have.
set_of_items = set(item) # set(['cat','fish'])
count = 0
for value in names:
if set_of_items & set(value) == set_of_items:
count+=1
return count

names =  [['cat', 'fish'], ['cat'], ['fish', 'dog', 'cat'],['cat', 'bird', 'fish'], ['fish', 'bird']]
# Now for each of your possibilities which you can generate
# Chain flattens the list, set removes duplicates, and combinations generates all possible pairs.
permuted_values = list(itertools.combinations(set(itertools.chain.from_iterable(names)), 2))
d = {}
for v in permuted_values:
d[str(v)] = listOccurences(v, names)
# The key in the dict being a list cannot be possible unless it's converted to a string.
print(d)
# {"['fish', 'dog']": 1, "['cat', 'dog']": 1, "['cat', 'fish']": 3, "['cat', 'bird']": 1, "['fish', 'bird']": 2}
``````
Friday, December 9, 2022