Count Same Values in Array and Combine into Array

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();

Merge duplicate and increase count for each object in array javascript

Make use of the feature that plus operator (+) converts boolean value true and false to number 1 and 0 respectively, and use OR (||) operator for assigning one of two values based on a condition, you can simplify your code as this:

const studentList = [
{code: "AA", gender:"male", DOB:"2000-05-15"},
{code: "AA", gender:"female", DOB:"2015-05-15"},
{code:"A0", gender:"female", DOB:"2005-01-01"},
{code: "A1", gender:"male", DOB:"2015-01-15"}
];



const result = new Map();
var nowYear = new Date().getYear();

studentList.forEach(e =>{
var item = result.get(e.code) || {code: e.code, count: 0, female: 0, child: 0 };
item.count++;
item.female += e.gender === "female";
item.child += nowYear - new Date(Date.parse(e.DOB)).getYear() < 18;
result.set(item.code, item);
})

for ([key, value] of result.entries()) {
console.log(value);
}

Merge objects in array by value and get count for each

I ended up going with this:

var groupedData = [];
_(typeData).groupBy('item').forEach(function(value, key) {
var obj = {};
obj.count = value.length;
obj.type = value[0].type;
obj.details = value[0].details;
obj.description = key;
groupedData.push(obj);
});

How to 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>';
}

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 combine arrays from a multidimensional array and count values?

This will produce the array you are looking for:

$out = array();
foreach( $data as $row ) {
if (!isset($out[$row['my_post_id']])) {
$out[$row['my_post_id']] = Array( "my_id"=>$row['my_id'],
"my_status" => $row["my_status"],
"my_rating" => $row["my_rating"]);
}
else {
$out[$row['my_post_id']]["my_status"] += $row["my_status"];
$out[$row['my_post_id']]["my_rating"] += $row["my_rating"];
}

}

results in:

Array
(
[123] => Array
(
[my_id] => 1
[my_status] => 3
[my_rating] => 14
)

[456] => Array
(
[my_id] => 4
[my_status] => 2
[my_rating] => 8
)

)


Related Topics



Leave a reply



Submit