How to Count Same Values in an Array and Store It to a Variable

How do I count occurrence of duplicate items in array

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
(
[12] => 1
[43] => 6
[66] => 1
[21] => 2
[56] => 1
[78] => 2
[100] => 1
)

How to count duplicate value in an array in javascript

function count() {    array_elements = ["a", "b", "c", "d", "e", "a", "b", "c", "f", "g", "h", "h", "h", "e", "a"];
array_elements.sort();
var current = null; var cnt = 0; for (var i = 0; i < array_elements.length; i++) { if (array_elements[i] != current) { if (cnt > 0) { document.write(current + ' comes --> ' + cnt + ' times<br>'); } current = array_elements[i]; cnt = 1; } else { cnt++; } } if (cnt > 0) { document.write(current + ' comes --> ' + cnt + ' times'); }
}
count();

How to count value inside array?

You could make use of a combination of explode, array_map, array_filter and array_count_values to split the values on a comma and keep the ones for which the values are 100.

For example:

$items = [
1 => [
"33,55,88,100,66,4",
"45,45,58,49,49,4",
"100,100,50,49,80,4"
],
2 => [
"33,55,88,100,66,4",
"45,45,58,49,49,4",
"100,100,50,49,80,4"
],
3 => [
"33,55,88,100,66,4",
"45,45,58,49,49,4",
"100,100,50,49,80,4"
],
];

foreach ($items as &$item) {
$item = array_map(function ($x) {
$res = array_filter(array_count_values(
explode(',', $x)
), function ($key) {
return $key === 100;
}, ARRAY_FILTER_USE_KEY);
return sprintf("100 (%s) Times", count($res) === 1 ? $res[100] : 0);
}, $item);
}

print_r($items);

Result

Array
(
[1] => Array
(
[0] => 100 (1) Times
[1] => 100 (0) Times
[2] => 100 (2) Times
)

[2] => Array
(
[0] => 100 (1) Times
[1] => 100 (0) Times
[2] => 100 (2) Times
)

[3] => Array
(
[0] => 100 (1) Times
[1] => 100 (0) Times
[2] => 100 (2) Times
)

)

Php demo

How to get count of duplicate objects in Array

You can easily achieve this using reduce and map. First, count all the occurrences of all objects and then add the property dynamically.

const arr = [
{
partNum: "ACDC1007",
brandName: "Electric",
supplierName: "Electric",
},
{
partNum: "ACDC1007",
brandName: "Electric",
supplierName: "Electric",
},
{
partNum: "ACDC1007",
brandName: "Electric",
supplierName: "Electric",
},
{
partNum: "ACDC1009",
brandName: "Electric",
supplierName: "Electric",
},

{
partNum: "ACDC1000",
brandName: "Electric",
supplierName: "Electric",
},
];

const countDict = arr.reduce((acc, curr) => {
const { partNum } = curr;
if (acc[partNum]) ++acc[partNum];
else acc[partNum] = 1;
return acc;
}, {});

const result = arr.map((obj) => {
obj["count"] = countDict[obj.partNum];
return obj;
});

console.log(result);

Javascript - Counting duplicates in object array and storing the count as a new object

I would stick to reduce, use a Map and spread its values to get the final result:

const names = [{  _id: 1 }, { _id: 1}, { _id: 2}, { _id: 1}];

const result = [...names.reduce( (mp, o) => {
if (!mp.has(o._id)) mp.set(o._id, { ...o, count: 0 });
mp.get(o._id).count++;
return mp;
}, new Map).values()];

console.log(result);

Count duplicate values in an array

Add this at the end of your code to only have duplicates in $count_values

$total_duplicates = 0;
foreach ($count_values as $key=>$count) {
if($count<=1){
unset($count_values[$key]);
}else{
$total_duplicates += $count;
}
}

This will remove any entries that are not duplicate (occuring once) via unset().
Also $total_duplicates will give you the total count of duplicates.

To output that as html do this:

echo '<p>You have '.$total_duplicates.' duplicates.</p>';
foreach ($count_values as $key=>$count) {
echo '<li>'.$key.' '.$count.'</li>';
}


Related Topics



Leave a reply



Submit