Wrie a function which returns the most frequent number in a
list of integers. Handle the case of more than one number
which meets this criterion.
public static int[] GetFrequency(int[] list)
Answer Posted / sudha chinnappa
I somehow feel that the code can be made more efficient. I
will leave this job to someone else
#include<iostream>
#include<conio.h> //for getch
using namespace std; //for cout
int main()
{
int arr[100];
cout << "Enter the No. of integers\n";
cin >> n ;
cout << "enter the numbers\n";
for(i=0; i<n; i++)
cin >> arr[i];
//Sort the numbers
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if( arr[i] > arr[j] )
{
arr[j] = arr[i] + arr[j];
arr[i] = arr[j] - arr[i];
arr[j] = arr[j] - arr[i];
}
}
}
struct node
{
int num;
int frq;
node *nxt;
};
struct node *first = NULL, *temp, *temp1,*temp2;
i=0;
while(i<n)
{
temp = new node;
j=1;
while(arr[i] == arr[i+1])
{j++; i++;}
temp->num = arr[i];
temp->frq = j;
temp->nxt = NULL;
if (first == NULL)
first = temp;
else
{
temp1 = first;
while(temp1->nxt != NULL)
temp1 = temp1->nxt;
temp1->nxt = temp;
}
i++;
}
temp1 = first;
int large = first->frq;
temp1 = first;
temp2 = first->nxt;
while(temp1->nxt != NULL)
{
if(temp1->frq < temp2->frq)
large =temp2->frq;
temp1 = temp1->nxt;
temp2 = temp2->nxt;
}
cout << "\nHighest frequency numbers:\n\n";
cout << "Number Frequency\n";
temp1 = first;
while(temp1 != NULL)
{
if(temp1->frq == large)
cout << temp1->num << " "<< temp1->frq
<< endl;
temp1 = temp1->nxt;
}
}
Is This Answer Correct ? | 10 Yes | 5 No |
Post New Answer View All Answers
What output does the following code generate? Why? What output does it generate if you make A::Foo() a pure virtual function? class A { A() { this->Foo(); } virtual void Foo() { cout << "A::Foo()" << endl; } }; class B : public A { B() { this->Foo(); } virtual void Foo() { cout << "A::Foo()" << endl; } }; int main(int, char**) { A objectA; B objectB; return 0; }
how to diplay a external image of output on winxp by using c & c++,
how to write a program that opens a file and display in reverse order?
find level of following tree (state, parent) " J,D I,D H,C E,B F,B G,C B,A D,A C,A A,& K,E L,E L,F M,F N,G O,H P,I P,H Q,I R,J S,K U,P T,L
write a program to perform generic sort in arrays?
How can I Draw an ellipse in 3d space and color it by using graph3d?
Write a C/C++ program that connects to a MySQL server and displays the global TIMEZONE.
Code for Two Classes for Doing Gzip in Memory?
write a program using virtual function to find the transposing of a square matrix?
write a program that prompt the user to enter his height and weight,then calculate the body mass index and show the algorithm used
Write a C++ program without using any loop (if, for, while etc) to print prime numbers from 1 to 100 and 100 to 1 (Do not use 200 print statements!!!)
i really need help about this.. write a program to display the set of odd and even numbers separately. find the highest and lowest value of the given numbers.
what mean void creat_object?in public class in this code class A{ public: int x; A(){ cout << endl<< "Constructor A";} ~A(){ cout << endl<< "Destructor A, x is\t"<< x;} }; void create_object(); void main() { A a; a.x=10; { A c; c.x=20; } create_object(); } void create_object() { A b; b.x=30; }
3. Program to find the Sum of give series. a. (1)+(1+2)+(1+2+3)+(1+2+3+4)+……………………………….. b. 1/1+1/9+1/25+1/49+……………...
Code for Small C++ Class to Transform Any Static Control into a Hyperlink Control?