Hi readers ,
Hope this article will help you to add some extra knowledge to you to learn programming so you must read the article full to get the important points from the article.
In this article we are going to discuss about Find the most occurred element in the array and number of occurrence.
We solve this problem by the help of C++ programming language If You do not know about C++ Programming don't worry just understand the logic i discussed below and you will get an idea .
The Below points mention are discussed in this article
- Question
- Logic to solve
- source code
- source code explanation
- output
- feedback
Question -
Find the most occurred element in the array and number of occurrence.
Logic to solve
- first declare a variable to display the the most occurred element .
- Then run two for loop one for loop is for all the elements and another for loop is for checking if there is any element is repeat in the array .
- if any element repeat then assign that value into the value that we declare first and then print it.
- Hope you understand this logic Lets go for the implementation
Source code
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int n;
int arr[10];
cout << "enter the size of array:" << endl;
cin >> n;
for (int i = 0; i < n; i++)
{
cout << "Enter the data no." << i + 1 << " : ";
cin >> arr[i];
}
int duplicate; //it is basically store the number
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++) //start from second value on array
if (arr[i] == arr[j])
{
duplicate = arr[i];
}
}
cout << "Most occured number is :" << duplicate << endl;
return 0;
}
Source code explanation
Output
enter the range - 6
2 2 4 5 2 3
The Most occur element is
4
The occurrence of its is
3
Also check Out
Also check Out Projects for Resume
Problems On C++
Q-2) Vectors In c++
Problems on TCS paper
1 , 1 , 2 , 3 , 4 , 9 , 8 , 27 , 16 , 81 , 32 , 243 , 64 , 729 , 128 , 2187
0 , 0 , 2 , 1 , 4 , 2 , 6 , 3 , 8 , 4 , 10 , 5 , 12 , 6 , 14 , 7 , 16 , 8
1 , 1 , 2 , 3 , 4 , 9 , 8 , 27 , 16 , 81 , 32 , 243 , 64 , 729 , 128 , 2187
0 , 0 , 2 , 1 , 4 , 2 , 6 , 3 , 8 , 4 , 10 , 5 , 12 , 6 , 14 , 7 , 16 , 8
FEEDBACK
If still you have any doubts please comment below and please share your approaches with us .
If still you have any doubts please comment below and please share your approaches with us .
Post a Comment
Post a Comment
Please do not spam in comment