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

Code for Easily Using Hash Table?

0 Answers  


Display Pattern: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 &#8230;

2 Answers   Mind Tree,


readers and writers problem

1 Answers   Cognizant,


Write A C++ Program To Input A Number Between 20 To 99 And Display Its Numbername?

3 Answers   TCS,


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

0 Answers  






how to find out the maximum number out of the three inputs.

6 Answers   ABC, Apple, C3I, HP, TCS,


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


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  


Write a program that print in screen a tree with its height taken from user by entering number of 4 digits and find the odd numbers then calculate the sum of odd numbers so he get the height of tree?

0 Answers  


write a program in c++ to scramble a bmp image file using a scramble key 0x7c and an XOR logic. print out the original image, the scrambled image and the program. Note: the first 24bytes of a bmp file contain the header information of the file.

1 Answers  


How to swap two ASCII numbers?

0 Answers  


Perform the functionality of 2-D array through 1-D array and in it the functions to be performed were: (1) Display the array in 2-D format (2) Display a particular element (3) Display a particular row (4) Display a particular column

1 Answers   Nagarro,


Categories