Question

So I need to write java program that find most occurring double value from double array....

So I need to write java program that find most occurring double value from double array.

for example if double array has {1, 2, 3, 5, 4, 5}

the result should be 5.0 since 5 appears two times.

if array has {1. 2, 2, 3, 3, 4}

result should return Double.NaN because both number 2 and 3 appears two times.

{1, 2, 3, 4, 5} this should return Double.NaN too because every numbers appears only once.

For some reason, when I have {1. 2, 2, 3, 3, 4}, the result always 3 when I should get NaN.

I cannot use java.util.Array

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts please please comment below.

Code:

import java.util.Scanner;

public class duplicate {
public static void main(String arg[])
{
Scanner sc = new Scanner(System.in);
System.out.println("enter arry size:");
int n = sc.nextInt(); // reading array size from user

double ar[] = new double[n]; // array for storing elements
int ar1[] = new int[n]; // array for storing counting of each element.

for (int i=0;i<n;i++) // loop for reading elements
{
ar[i] = sc.nextDouble(); // reading double values from user
}

for(int i=0;i<n-1;i++) // loop for sorting array
{
for(int j=0;j<n-i-1;j++)
{
if(ar[j]>ar[j+1])
{
double temp = ar[j];
ar[j] = ar[j+1];
ar[j+1] = temp;
}
}
}
for(int i=0;i<n;i++) // loop for finding frequency of each element
{
for(int j=0;j<n;j++)
{
if(ar[i]==ar[j])
{
ar1[i] += 1;
}
}
}
double c = 0,c1=ar1[0];
int c2=0;

for(int i=0;i<n-1;i++)
{
if(ar1[i]>1 && ar1[i]!=ar[i+1]) // condition for checking multiple duplicates
{
c++;
}
if(c1<ar1[i])
{
c1 = ar1[i];
c2 = i;
}
}
if(c>1 || c==0) // if all element are occurred once or occurred multiple times
{
System.out.println("Double"); // printing Double
}
else
{
System.out.println(ar[c2]); // if only one element occurred mores time printing that element
}
}

}


Add a comment
Know the answer?
Add Answer to:
So I need to write java program that find most occurring double value from double array....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need to create a Java program that, given an array of size N whose entries...

    I need to create a Java program that, given an array of size N whose entries are numbers between 0 and 10^6, finds the largest repeating subarray. Doesn't matter how many times the array is repeated. For example, an array [0 1 2 1 4 1 2 1 0 5] should return 3 for the subarray [1 2 1]. I need this to be quite time efficient. I was recomended Suffix array but no idea how to implement.

  • Hello Sir, I need help with Java. Here is the problem below. Problem 1.Write a program...

    Hello Sir, I need help with Java. Here is the problem below. Problem 1.Write a program that prompts the user to enter the number of milliseconds and converts the milliseconds to a string hours:minutes:seconds. The program should use the convertMillismethod with the following header: public static String convertMillis(long millis) For example, convertMillis(5500) returns the string 0:0:5, convertMillis(100000) returns the string 0:1:40, and convertMillis(555550000) returns the string 154:19:10. Problem 2. (Count occurrence of numbers)Write a program that reads integers between 1...

  • Please write in Java and Array list. 3 THE COUNT LIKE PROBLEM Start with 1 String...

    Please write in Java and Array list. 3 THE COUNT LIKE PROBLEM Start with 1 String array wordList and one String word. Your method should calculate and return the number of times that word appears in the array wordList. countLike({"a", "a", "b", "c"}, "a") → 2 countLike({"a", "a", "b", "c"}, "c") → 1 countLike({"a", "a", "b", "c"}, "3") → 0 4 THE AVERAGE EVENS PROBLEM Start with an int array named nums. Return the average of all the even numbers...

  • Coded in Java. Also advised to use class java.util.HashMap Problem 1 You are given an array...

    Coded in Java. Also advised to use class java.util.HashMap Problem 1 You are given an array A of integers and an integer k. Implement an algorithm that determines, in linear time, the smallest integer that appears at least k times in A. Problem 2 You are given an array A of integers and an integer k. Implement an algorithm that determines in linear time whether there are two distinct indices i and j in the array such that A[i] =...

  • Java Programming. Write your own source code with comments. (Print distinct numbers) Write a program that...

    Java Programming. Write your own source code with comments. (Print distinct numbers) Write a program that reads in ten numbers and displays the number of distinct numbers and the distinct numbers separated by exactly one space (i.e., if a number appears multiple times, it is displayed only once). (Hint: Read a number and store it to an array if it is new. If the number is already in the array, ignore it.) After the input, the array contains the distinct...

  • Java programming: I need to create a method that is given a 2D char array and...

    Java programming: I need to create a method that is given a 2D char array and a String that returns a part of the original 2D array. The input string will tell the method which rows from the input array need to be returned. For example, a 5x5 char array input along with a string "0 4" would return rows 1 and 5 of the input array. The String input will always be numbers divided by spaces and will not...

  • public static double[] getVolumes(double[] base, double[] height, double[] length) Write a Java program called TriangularPrisms which...

    public static double[] getVolumes(double[] base, double[] height, double[] length) Write a Java program called TriangularPrisms which does the following: In the main method: Use a for loop which repeats three times. Within the loop, a. prompt the user to enter the base of the triangular prism and store the value in a double array called base b. prompt the user to enter the corresponding height and store the value in a double array called height c. prompt the user to...

  • Write a Java program to input a number of values into an array and then process...

    Write a Java program to input a number of values into an array and then process the array by passing it to various methods that will compute and return information we are interested in. These include: 1. the average of the values in the array 2. the standard deviation of the values in the array 3. the number of items in the array less than the average 4. whether the array’s values are in increasing order or not First, you...

  • 1.Write a Java program that computes the average of the values of a row in a...

    1.Write a Java program that computes the average of the values of a row in a twodimensional array. You should create a method with the following header: public static double averageRow(double[][] array, int row) Write a test program that reads a matrix from the user and a row index to calculate the average on. Print the result. 2.A square matrix is said to be an upper triangular matrix if the value of the cell is 0 when row > column....

  • I NEED A BASIC JAVA PROGRAM USING ARRAY TO FIND THE AVERAGE FOR 10 STUDENTS. THEN...

    I NEED A BASIC JAVA PROGRAM USING ARRAY TO FIND THE AVERAGE FOR 10 STUDENTS. THEN THERE SHOULD BE A COUNTER FOR STUDENTS AND AN ACCUMULATOR FOR THE AVERAGE AND INPUT VALIDATION FOR THE THREE TEST SCORES; THEY MUST BE BETWEEN 0 TO 100. ALSO THE AVERAGE AND AVERAGE ACCUMULATOR SHOULD BE FORMATTED TO TWO DECIMAL PLACES AND AT THE END ALL 10 STUDENTS SHOULD BE IN A KIND OF A TABLE SHOWING FIRST NAME, MIDDLE INITIAL, LAST NAME TEST...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT