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
Given a table of the form: Product Sold on A 1/1/1980 B 1/1/1980 C 1/1/1980 A 1/1/1980 B 1/1/1980 C 2/1/1980 A 2/1/1980 There are 30 products and 10,000 records of such type. Also the month period during which sales happened is given to u. Write the program to display the result as: Product Month No. of copies A January 12 A February 15 A March 27 B January 54 B February 15 B March 10 C January 37
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!!!)
solve the problem in the programming language C++"if a five digit number is input through the keyboard.Write a program to calculate the sum of its digits(hint: use the modulus operator)
Write a (n) algorithm that sorts n distinct integers, ranging in size between 1 and kn inclusive, where k is a constant positive integer. (Hint: Use a kn-element array.)
Performance Algorithm A performs 10n2 basic operations and algorithm B performs 300 lg n basic operations. For what value of n does algorithm B start to show its better performance?
write a program to calculate the amount of investment after a period n years if the principal investors was p and interest is calculated using compound interest,formular=a=p(1+r)^n
write a program that reverses the input number of n.Formulate an equation to come up with the answer.
create a stucture student containing field for roll no,class,year and marks.create 10 student annd store them in a file
Code for Two Classes for Doing Gzip in Memory?
Code for Method of Handling Factorials of Any Size?
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; }
write a program to convert temperature from fa height into celcius and vise versa,use modular programming
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
What output does this program generate as shown? Why? class A { A() { cout << "A::A()" << endl; } ~A() { cout << "A::~A()" << endl; throw "A::exception"; } }; class B { B() { cout << "B::B()" << endl; throw "B::exception"; } ~B() { cout << "B::~B()"; } }; int main(int, char**) { try { cout << "Entering try...catch block" << endl; A objectA; B objectB; cout << "Exiting try...catch block" << endl; } catch (char* ex) { cout << ex << endl; } return 0; }
write a program that creates a sequenced array of numbers starting with 1 and alternately add 1 and then 2 to create the text number in the series , as shown below. 1,33,4,6,7,9,............147,148,150 Then , using a binary search , searches the array 100 times using randomly generated targets in the range of 1 to 150