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)

Answers were Sorted based on User's Feedback



Wrie a function which returns the most frequent number in a list of integers. Handle the case of m..

Answer / 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

Wrie a function which returns the most frequent number in a list of integers. Handle the case of m..

Answer / paras malik

//I strongly feel that this signature is of java instead of
c++ with wrong signature name because function name
according to java conventions should start with small letter.

import java.util.HashMap;
import java.util.Set;

class A{



public static void main(String [] a){


int array[]={1,1,2,1,2,3,4,5,2,3,1};

array = GetFrequency(array);


for(int i =0;i<array.length;i++){

System.out.println(array[i] + "\n");

}

}



public static int[] GetFrequency(int [] array){

HashMap<Integer ,Integer > map = new HashMap<Integer,Integer>();



for(int i =0;i<array.length;i++){


if(map.get(array[i])==null) map.put(array[i],1);
else{
int k = map.get(array[i]);
map.put(array[i],k+1);

}

}

int a[] = new int[map.size()];

Set<Integer> set= map.keySet();
int i =0;
for(int s : set)
a[i++]=map.get(s);
return a;




}

}

Is This Answer Correct ?    5 Yes 0 No

Wrie a function which returns the most frequent number in a list of integers. Handle the case of m..

Answer / rob lange

// I added a size input because I don't know how it would
// work without it. I also don't know what you're going
// to do without the returned size information, but whatever.
int* FindMax( int * list, int size )
{
// Check bad input conditions
if ( size == 0 )
return NULL;
else if ( size == 1 )
return list;

// Turn int pointer list into vector
int* curr = list;
std::vector< int > vList;
while ( size != 0 )
{
vList.push_back( *curr );
curr++;
size--;
}

// Sort
sort( vList.begin(), vList.end() );
vList.push_back( 0 ); // dummy tail node

// Find most frequent number
std::vector<int>::iterator i = vList.begin();
int curMaxLength = 0;
int tmpLength = 0;
std::vector<int> MostFreqNums;
while ( i+1 != vList.end() )
{
if ( *i == *(i+1) )
tmpLength++;
else
{
if ( tmpLength > curMaxLength )
{
MostFreqNums.clear();
MostFreqNums.push_back( *i );
curMaxLength = tmpLength;
}
else if ( tmpLength == curMaxLength )
{
MostFreqNums.push_back( *i );
}
tmpLength = 0;
}
++i;
}

// Convert vector list to int pointer array
int * ret = new int[ MostFreqNums.size() ];
i = MostFreqNums.begin();
for( int j = 0; i != MostFreqNums.end(); ++i, ++j )
{
ret[j] = *i;
}

return ret;
}

Is This Answer Correct ?    1 Yes 3 No

Post New Answer

More C++ Code Interview Questions

write a program to perform generic sort in arrays?

0 Answers  


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

0 Answers  


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?

0 Answers   ASD Lab, Qatar University, UNV,


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

0 Answers  


How do I store linked list datas into an array?

1 Answers  






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++

0 Answers  


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.

0 Answers  


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

0 Answers  


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.

0 Answers   ABC, Guidance Software,


. Remove all the blank spaces between character.Matrix is of 10* 10. eg: INPUT ------------------------------------ | N | A | | V | |T ------------------------------------- | |G | U | |P | -------------------------------------- |T | | | A | | ------------------------------------ OUTPUT: ------------------------------------ | N | A | V | T | | ------------------------------------- |G |U | P | | | -------------------------------------- |T | A | | | | ------------------------------------

2 Answers   Nagarro,


write a function – oriented program that calculates the sum of the squares from 1 to n. thus, if the input is 3, the output is 14

3 Answers  


write a program that reverses the input number of n.Formulate an equation to come up with the answer.

0 Answers   Satyam,


Categories