Find the frequency of presence of an
element in an array???
Agenda
1. Program in c++
2. Array declaration and definition
3. Enter the element to be counted
4. Counter or freq variable declaration
5. Print the particular element along with a
number how many times that number gets
repeated in the given array
Logic
• Given array for example:
• arr[]={1,2,3,2,3,2,2}
• Element to be find is 2 (No.of times repeated)
• How to find the repeated element present in an
array
• Soln:Using for loop:
• for (i=0;i<n;i++)
• if(a[i]==ele)(main logic)
• counter++;
Let’s See Practically by writing the
program
• #include<iostream.h>
• #include<conio.h>
• void main()
• {
• int a[100],ele,n,i,freq;
• clrscr();
• cout<<"Enter the size of array"<<endl;
• cin>>n;
• cout<<"Enter the elements of array"<<endl;
• for(i=0;i<n;i++)
• {
• cin>>a[i];
• }
• cout<<"Enter the element to be counted"<<endl;
• cin>>ele;
• freq=0;
• for(i=0;i<n;i++)
• {
• if(a[i]==ele)
• {
• freq=freq+1;
• }
• }
• cout<<"Frequency of "<<ele<<" is "<<freq;
• getch();
• }
Thank You

Frequency count of the Element in an array

  • 1.
    Find the frequencyof presence of an element in an array???
  • 2.
    Agenda 1. Program inc++ 2. Array declaration and definition 3. Enter the element to be counted 4. Counter or freq variable declaration 5. Print the particular element along with a number how many times that number gets repeated in the given array
  • 3.
    Logic • Given arrayfor example: • arr[]={1,2,3,2,3,2,2} • Element to be find is 2 (No.of times repeated) • How to find the repeated element present in an array • Soln:Using for loop: • for (i=0;i<n;i++) • if(a[i]==ele)(main logic) • counter++;
  • 4.
    Let’s See Practicallyby writing the program
  • 5.
    • #include<iostream.h> • #include<conio.h> •void main() • { • int a[100],ele,n,i,freq; • clrscr(); • cout<<"Enter the size of array"<<endl; • cin>>n; • cout<<"Enter the elements of array"<<endl; • for(i=0;i<n;i++) • { • cin>>a[i]; • } • cout<<"Enter the element to be counted"<<endl; • cin>>ele; • freq=0; • for(i=0;i<n;i++) • { • if(a[i]==ele) • { • freq=freq+1; • } • } • cout<<"Frequency of "<<ele<<" is "<<freq; • getch(); • }
  • 6.