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
output for printf("printf");
How can I Draw an ellipse in 3d space and color it by using graph3d?
develop a program to calculate and print body mass index for 200 employees
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; }
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
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
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)
What output does the following code generate? Why? What output does it generate if you make A::Foo() a pure virtual function? class A { A() { this->Foo(); } virtual void Foo() { cout << "A::Foo()" << endl; } }; class B : public A { B() { this->Foo(); } virtual void Foo() { cout << "A::Foo()" << endl; } }; int main(int, char**) { A objectA; B objectB; return 0; }
write a program that reverses the input number of n.Formulate an equation to come up with the answer.
write a program using virtual function to find the transposing of a square matrix?
Write a (n) algorithm that sorts n distinct integers, ranging in size between 1 and kn inclusive, where k is a constant positive integer. (Hint: Use a kn-element array.)
write a program using 2 D that searches a number and display the number of items 12 inputs values input 15,20, 13, 30, 38, 40,16, 18, 20 ,18 ,20 enter no. to search : 20
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+……………...
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.
how to take time as input in the format (12:02:13) from user so that controls remains between these columns?