answersLogoWhite

0

You can do this with a simple for loop counting through the array and increasing a tally each time you sought number is hit.:

tally = 0
for each element in the array {
if this element has the value we want{
tally = tally + 1

}

}
output the taly


In PHP for example, this could be done like so:

$tally = 0;
foreach($foo as $bar){
if($bar == $value_sought) $tally++;

}
echo $tally;

where "$foo" is the array you're serching and $value_sought is the number you're looking for.

User Avatar

Wiki User

15y ago

Still curious? Ask our experts.

Chat with our AI personalities

BeauBeau
You're doing better than you think!
Chat with Beau
DevinDevin
I've poured enough drinks to know that people don't always want advice—they just want to talk.
Chat with Devin
FranFran
I've made my fair share of mistakes, and if I can help you avoid a few, I'd sure like to try.
Chat with Fran

Add your answer:

Earn +20 pts
Q: How could you count a number how many times present in an array?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How do you count the number of elements in an array using PHP?

Using the function "count". <?php $foo = array("John", "Jacob", "Jingleheimer", "Schmidt"); echo count($foo); // <-- outputs the number 4 ?>


How can you calculate the average and median in perl by subroutine?

<pre> sub average { @_ 1 or die ('Sub usage: $median = median(\@array);'); my ($array_ref) = @_; my $count = scalar @$array_ref; # Sort a COPY of the array, leaving the original untouched my @array = sort { $a <=> $b } @$array_ref; if ($count % 2) { return $array[int($count/2)]; } else { return ($array[$count/2] + $array[$count/2 - 1]) / 2; } } </pre>


How do you find the average of rows and columns in a 2d array in java?

You add up all the array elements, then divide by the number of elements. You can use a nested for() loop in Java; inside the inner for() loop, you can both increase a counter (to count how many elements there are), and add to a "sum" variable.


How you count lentgh of string without using strlen?

Basically in C language string is NULL (0x00) byte ending char array. So in order to find out the length of the string you need to count all elements in array until you reach NULL. But that is what strlen does. There are two links with information about strlen implementation and null-terminated strings.


Write a 'c' program to accept 'n' numbers from user store these number into an array count the numbers of occurrence of each numbers?

#include<stdio.h> #include<conio.h> void main() { int a[10],i,j,k; int count=1,num[10],pos=0; clrscr(); printf("Enter the Array Element "); for(i=0;i<10;i++) { scanf("%d",&a[i]) ; }//close the for loop for(i=0;i<10;i++) { count=1,pos++; for(j=0;j<10;j++) { if(a[i]==a[j]) { for(k=j;k<10;j++) a[k]=a[k+1] }//close the if count++; }//close the for loop num[pos] = count; }//close the for loop for(i=0;i<pos;i++) printf("Repeted Number of IN Arrary %d",num[i]); }//close the main