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)

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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

write a program that creates a sequenced array of numbers starting with 1 and alternately add 1 and then 2 to create the text number in the series , as shown below. 1,33,4,6,7,9,............147,148,150 Then , using a binary search , searches the array 100 times using randomly generated targets in the range of 1 to 150

3270


write a program that can LOCATE and INSERT elements in array using c++ programming languages.

3443


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.

3400


how to write a program that opens a file and display in reverse order?

2571


Code for Method of Handling Factorials of Any Size?

2008






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

2370


How to Split Strings with Regex in Managed C++ Applications?

3132


Given a table of the form: Product Sold on A 1/1/1980 B 1/1/1980 C 1/1/1980 A 1/1/1980 B 1/1/1980 C 2/1/1980 A 2/1/1980 There are 30 products and 10,000 records of such type. Also the month period during which sales happened is given to u. Write the program to display the result as: Product Month No. of copies A January 12 A February 15 A March 27 B January 54 B February 15 B March 10 C January 37

2200


Teta-Omeg-Big-Oh Show that f(n) = n2 + 3n3 is ;(n3).

3195


3. Program to find the Sum of give series. a. (1)+(1+2)+(1+2+3)+(1+2+3+4)+……………………………….. b. 1/1+1/9+1/25+1/49+……………...

4355


can you please write a program for deadlock that can detect deadlock and to prevent deadlock.

2748


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

2845


write a function that reverse the elements of an array in place.The function must accept only one pointer value and return void.

3964


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.

1972


write a program that reads a series of strings and prints only those strings begging with letter "b"

2674