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


Please Help Members By Posting Answers For Below Questions

Code for Two Classes for Doing Gzip in Memory?

2814


How can I Draw an ellipse in 3d space and color it by using graph3d?

2138


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

2198


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?

7344


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

2051






Question 1: Implement a base class Appointment and derived classes Onetime, Daily, Weekly, and Monthly. An appointment has a description (for example, “see the dentist”) and a date and time. Write a virtual function occurs_on(int year, int month, int day) that checks whether the appointment occurs on that date. For example, for a monthly appointment, you must check whether the day of the month matches. Then fill a vector of Appointment* with a mixture of appointments. Have the user enter a date and print out all appointments that happen on that date. *This Should Be Done IN C++

553


Create a program to read two random data set in two files named data1.txt and data2.txt manifold contains integer numbers, whereas data2.txt file contains the float type numbers. Simpanlahmasing each into 2 pieces of data that is an array of type integer array and an array of type float, then calculate the average numbers in the second array.

2152


Implement a command console for changing settings on a particular object. The command console should allow you to enter a string and will return the response (very similar to a terminal session). The commands are as follows: SET propertyname=newvalue will change the target object’s member named “propertyname” to have a value equal to “newvalue”. If the input value is incompatible (i.e. an int being set to a string), print out an appropriate error message. GET propertyname will print out the current value of the target object’s member named “propertyname”. GET * will print out a list of all target object members and their current values. The system should be extensible for future commands and should accept an arbitrary object, such that another developer could insert another object into the system and rely on the command console to get and set the properties correctly.

3398


U hv to enter a range from a and b and search hw many no. of times a pattern n. occurs between the range a and b. Eg :i/p:enter range :0 100 Enter pattern: 13 o/p: the no. times 13 occurred betwwn 0 to 100:1 Eg :i/p:enter range :100 1000 Enter pattern: 13 o/p: the no. times 13 occurred betwwn 100 to 1000: (in this 13,113,131,132,133…139,213,313,…913 all these will be counted)

2126


Ask the user to input three positive integers M, N and q. Make the 2 dimensional array of integers with size MxN, where all the elements of I (I = 1,…,M) line will be members of geometrical progression with first element equal to the number of line (I) and denominator q.

3397


Code for Small C++ Class to Transform Any Static Control into a Hyperlink Control?

2565


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; }

2069


write a program using virtual function to find the transposing of a square matrix?

2845


Write a C/C++ program that connects to a MySQL server and checks if the InnoDB plug-in is installed on it. If so, your program should print the total number of disk writes by MySQL.

2067


We need to write the function to check the password entered is correct or not based on the following conditions.. a) It must have atleast one lower case character and one digit. b)It must not have any Upper case characters and any special characters c) length should be b/w 5-12. d) It should not have any same immediate patterns like abcanan1 : not acceptable coz of an an pattern abc11se: not acceptable, coz of pattern 11 123sd123 : acceptable, as not immediate pattern adfasdsdf : not acceptable, as no digits Aasdfasd12: not acceptable, as have uppercase character

3957