Occurrence of item in array in C++ by R4R Team

Array:
Collection of different values having same data type is known as Array.

Example-
Array is 1 2 5 9 1 8 9 1
Occurrence of 1 is 3
Occurrence of 2 is 1
Occurrence of 9 is 2

program:

#include< iostream>
int main()
{
int a[100],n,count=0,num,i;
cout<<"Number of elements in array ?"<< endl;
cin>>n;
//input in array
cout<<"Enter "<< n<<" number in array"<< endl;
for(i=0;i< n;i++)
cin>>a[i];
cout<<"Enter number to search"<< endl;
cin>>num;
//Find occurrence
for(i=0;i< n;i++)
{
if(num==a[i])
count++;
}
cout<<"Occurrence of "<< num<<" in array is "<< count;
}


output-

Number of elements in array ?
6
Enter 6 number in array
1 3 2 1 2 3
Enter number to search
1
occurrence of 1 in array is 2

Number of elements in array ?
6
Enter 6 number in array
1 3 2 1 2 3
Enter number to search
8
occurrence of 8 in array is 0

-In this program, we take a input in the array of size n.
-We run loop n times for the array and search whole array for particular number and count




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!