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
can you please write a program for deadlock that can detect deadlock and to prevent deadlock.
how to write a program that opens a file and display in reverse order?
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
Code for Method of Handling Factorials of Any Size?
write a program that reverses the input number of n.Formulate an equation to come up with the answer.
develop a program to calculate and print body mass index for 200 employees
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+……………...
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)
Code for Easily Using Hash Table?
output for printf("printf");
How to swap two ASCII numbers?
write a program that reads a series of strings and prints only those strings begging with letter "b"
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/C++ program that connects to a MySQL server and displays the global TIMEZONE.