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
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 |
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 |
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 |
PROBLEM #8 The cashier at the counter of a Super Store, Mr. Khazaanchi has the following bundles of rupee cash notes with him: Rs. 1, 2, 5, 10, 50, 100, 500, 1000 A customer comes at his counter with various items that he has shopped. Mr. Khazaanchi totals the item prices and tells the customer his total amount payable. The customer gives Mr. Khazanchi some amount of cash. Find the total number of rupee notes of each denomination (i.e. 1, 2, 5, 10, 50, 100, 500, 1000) Mr. Khazaanchi will have to give to the withdrawer ensuring that the total number of rupee notes are minimum.
how to find out the maximum number out of the three inputs.
6 Answers ABC, Apple, C3I, HP, TCS,
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)
write a function that reverse the elements of an array in place.The function must accept only one pointer value and return void.
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.
Here's the programm code: int magic(int a, int b) { return b == 0 ? a : magic(b, a % b); } int main() { int a, b; scanf("%d%d", &a, &b); printf("%d\n", magic(a, b)); return 0; } on input stream we have integers 4, 45 What's the output integer? How many times will be initiated "magic" function?
How do I store linked list datas into an array?
i really need help about this.. write a program to display the set of odd and even numbers separately. find the highest and lowest value of the given numbers.
write a program that prompt the user to enter his height and weight,then calculate the body mass index and show the algorithm used
0 Answers Jomo Kenyatta University,
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
Code for Method of Handling Factorials of Any Size?
write a program to sort 'n' elemnts using bubble sort