Answers for "how to count an array"

PHP
30

php length of array

<?php
	$arr = ["one", "two", "three", "four"];
	echo count($arr);
  ?>
Posted by: Guest on December-29-2019
0

Count elements in an array

//#Source https://bit.ly/2neWfJ2 
const countOccurrences = (arr, val) => arr.reduce((a, v) => (v === val ? a + 1 : a), 0);
console.log(countOccurrences([1, 1, 2, 1, 2, 3], 1));
console.log(countOccurrences([1, 1, 2, 1, 2, 3], 2));
console.log(countOccurrences([1, 1, 2, 1, 2, 3], 3));
Posted by: Guest on April-20-2021

Code answers related to "how to count an array"

Browse Popular Code Answers by Language