Count How Many Times Elements in an Array Are Repeated

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

Finding number of repeated elements in an array using C

To avoid over-counting, you will need to keep track of which numbers have already been repeated. I wold just have a second array to save "already repeated" values:

#include <stdio.h>

int main()
{
int n, p = 0, r = 0;

printf("Enter the size of the array: ");
scanf(" %u",&n);
int arr[n];
int rep[n - 1];
for (int i = 0; i < n; i++) {
printf("Enter arr[%d]: ",i);
scanf(" %d",&arr[i]);
}
for (int i = 0; i < n; i++) {
int count = 1, j;
for (j = 0; j < r; j++) {
if (arr[i] == rep[j])
break;
}
if (j < r)
continue;
for (j = i + 1; j < n; j++)
if (arr[i] == arr[j])
count++;
if (count > 1) {
p = p + count;
rep[r++] = arr[i];
}
}
printf("Number of repitions is %d\n",p);
}

Count the number of times a same value appears in a javascript array

There might be different approaches for such purpose.
And your approach with for loop is obviously not misplaced(except that it looks redundantly by amount of code).
Here is some additional approaches to get the occurrence of a certain value in array:

  • Using Array.forEach method:

    var arr = [2, 3, 1, 3, 4, 5, 3, 1];

    function getOccurrence(array, value) {
    var count = 0;
    array.forEach((v) => (v === value && count++));
    return count;
    }

    console.log(getOccurrence(arr, 1)); // 2
    console.log(getOccurrence(arr, 3)); // 3
  • Using Array.filter method:

    function getOccurrence(array, value) {
    return array.filter((v) => (v === value)).length;
    }

    console.log(getOccurrence(arr, 1)); // 2
    console.log(getOccurrence(arr, 3)); // 3

How do I count occurrence of duplicate items in array [duplicate]

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
)

Counting the occurrences / frequency of array elements

const arr = [2, 2, 5, 2, 2, 2, 4, 5, 5, 9];

function foo (array) {
let a = [],
b = [],
arr = [...array], // clone array so we don't change the original when using .sort()
prev;

arr.sort();
for (let element of arr) {
if (element !== prev) {
a.push(element);
b.push(1);
}
else ++b[b.length - 1];
prev = element;
}

return [a, b];
}

const result = foo(arr);
console.log('[' + result[0] + ']','[' + result[1] + ']')
console.log(arr)

Counting repeated elements in an integer array

This kind of problems can be easy solved by dictionaries (HashMap in Java).

  // The solution itself 
HashMap<Integer, Integer> repetitions = new HashMap<Integer, Integer>();

for (int i = 0; i < crr_array.length; ++i) {
int item = crr_array[i];

if (repetitions.containsKey(item))
repetitions.put(item, repetitions.get(item) + 1);
else
repetitions.put(item, 1);
}

// Now let's print the repetitions out
StringBuilder sb = new StringBuilder();

int overAllCount = 0;

for (Map.Entry<Integer, Integer> e : repetitions.entrySet()) {
if (e.getValue() > 1) {
overAllCount += 1;

sb.append("\n");
sb.append(e.getKey());
sb.append(": ");
sb.append(e.getValue());
sb.append(" times");
}
}

if (overAllCount > 0) {
sb.insert(0, " repeated numbers:");
sb.insert(0, overAllCount);
sb.insert(0, "There are ");
}

System.out.print(sb.toString());

Python: count repeated elements in the list

You can do that using count:

my_dict = {i:MyList.count(i) for i in MyList}

>>> print my_dict #or print(my_dict) in python-3.x
{'a': 3, 'c': 3, 'b': 1}

Or using collections.Counter:

from collections import Counter

a = dict(Counter(MyList))

>>> print a #or print(a) in python-3.x
{'a': 3, 'c': 3, 'b': 1}


Related Topics



Leave a reply



Submit