Answers for "count of an element in an array"

PHP
2

mark occurances of elements in array cpp

int sumOfDistinct(int a[], int n){
  	int sum = 0; 
    for (int i = 0; i < n; i++) { 
      	 // If element appears first time 
        if (a[abs(a[i]) - 1] >= 0) { 
            sum += abs(a[i]); 
            a[abs(a[i]) - 1] *= -1; 
        } 
    } 
    return sum;
Posted by: Guest on May-02-2020
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 "count of an element in an array"

Browse Popular Code Answers by Language