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 C++ Program To Input A Number Between 20 To 99 And Display Its Numbername?

3 Answers   TCS,


Coin Problem You are given 9 gold coins that look identical. One is counterfeit and weighs a bit greater than the others, but the difference is very small that only a balance scale can tell it from the real one. You have a balance scale that costs 25 USD per weighing. Give an algorithm that finds the counterfeit coin with as little weighting as possible. Of primary importance is that your algorithm is correct; of secondary importance is that your algorithm truly uses the minimum number of weightings possible. HINT: THE BEST ALGORITHM USES ONLY 2 WEIGHINGS!!!

1 Answers   Motorola, Qatar University,


Write a program that takes a 3 digit number n and finds out whether the number 2^n + 1 is prime, or if it is not prime find out its factors.

2 Answers   TCS,


write a function that allocates memory for a single data type passed as a parameter.the function uses the new operator and return a pointer to the allocated memory.the function must catch and handle any exception during allocation

0 Answers   HCL,


Write a program that takes a 3 digit number n and finds out whether the number 2^n + 1 is prime, or if it is not prime find out its factors.

5 Answers   ADP, Amazon, HCL, IBM, Infosys, Satyam, TCS, Vimukti Technologies,






how to diplay a external image of output on winxp by using c & c++,

0 Answers  


Write code for the multiplication of COMPLEX numbers?

0 Answers   IBM,


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&#8230;139,213,313,&#8230;913 all these will be counted)

0 Answers  


hello friends, given an expression we have to remove the unwanted brackets in that expression. Eg : (a+b) ---> a+b (a+b)*(c)-----> (a+b)*c. Please mail me if you know the logic. My mail id is : saravana6m@gmail.com. Thank you in advance :-)

1 Answers   GrapeCity, Microsoft,


i don't know about working of nested for loop can any one help me

0 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  


. 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,


Categories