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

what is virtual constroctor ? give an exam for it?-(parimal dhimmar)

2 Answers  


#include<stdio.h> #include<conio.h> void main() { char str[10]; int,a,x,sw=0; clrscr(); printf("Enter a string:"); gets(str); for(x=0;x<=a;a++); for(x=0;x<=a;x++) { if(str[x]==str[a-1-x]) { sw=1; } else sw=0; } if(sw==10 printf("The entered string is palindrome:"); else printf("The entered string is not a palindrome:); } getch(); } wht would be the explanation with this written code???

2 Answers  


Write a C/C++ program that connects to a MySQL server and displays the global TIMEZONE.

0 Answers   Facebook, Webyog, Wipro,


Code for Easily Using Hash Table?

0 Answers  


What is the time complexity T(n) of the following program? a) int n, d, i, j; cin >> n; for (d=1; d<=n; d++) for (i=1; i<=d; i++) for (j=1; j<=n; j += n/10) cout << d << " " << i << " " << j << endl; b) void main() { int n, s, t; cin >> n; for (s = 1; s <= n/4; s++) {t = s; while (t >= 1) { cout << s << " " << t << endl; t--; } } } c) void main() { int n, r, s, t; cin >> n; for (r = 2; r <= n; r = r * 2) for (s = 1; s <= n/4; s++) { t = s; while (t >= 1) { cout << s << " " << t << endl; t--; } } }

3 Answers   CTS, IBM, Infosys, 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,


Complexity T(n) Write a linear-time algorithm that sorts n distinct integers, each of which is between 1 and 500. Hint: Use a 500-element array. (Linear-time means your algorithm runs in time c*n + b, where c and b are any constants that do not depend on n. For example, your algorithm can run in time n, or time 2n + 1, or time 5n + 10, or time 100n + 6, but not time c*n*n = c*n?.)

1 Answers   Qatar University,


Counting in Lojban, an artificial language developed over the last fourty years, is easier than in most languages The numbers from zero to nine are: 0 no 1 pa 2 re 3 ci 4 vo 5 mk 6 xa 7 ze 8 bi 9 so Larger numbers are created by gluing the digit togather. For Examle 123 is pareci Write a program that reads in a lojban string(representing a no less than or equal to 1,000,000) and output it in numbers.

4 Answers  


Subsets Write an algorithm that prints out all the subsets of 3 elements of a set of n elements. The elements of the set are stored in a list that is the input to the algorithm. (Since it is a set, you may assume all elements in the list are distinct.)

1 Answers   CSC, Qatar University,


main(){int a=5,b 10,c=2, d;a=b c;d=++a=(--c)*2; printf("%d%d%d%d,a,b,c,d; return o;}

1 Answers  


write a program to calculate the amount of investment after a period n years if the principal investors was p and interest is calculated using compound interest,formular=a=p(1+r)^n

0 Answers   Jomo Kenyatta University,


using friend function find the maximum number from given two numbers from two different classes.write all necessary functions and constructor for the classes.

1 Answers   TCS,


Categories