Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that reads a list of real numbers. After the program ends it sho

ID: 3549827 • Letter: W

Question

Write a program that reads a list of real numbers. After the program ends it should print out only the unique numbers. That is, only numbers that appear once in the list. If there are more than 50 unique numbers on the list, then you should only print the first 50. Currently have... import java.util.*; import java.io.*; import java.util.Arrays; public class Hmwk { public static void main(String[] args) throws FileNotFoundException { Scanner input=new Scanner (new File ("input.txt")); int n = 0; final int MAX_SIZE = 50; double[] numbersArray = new double[MAX_SIZE]; while (input.hasNextDouble() && n

Explanation / Answer

import java.util.Scanner;

import java.util.*;

import java.io.*;

// Save this code in file named Solution.java to run it

public class Solution {


// The solution uses a set to store the number

// And an arraylist to maintain the ordering

// The number is added into the set and the array list only if it is not there in the set

int MAX = 50;

public static void main(String[] args) throws Exception{


Scanner input = new Scanner(new FileInputStream("input.txt"));

Set<Integer> set = new HashSet<Integer>();

ArrayList<Integer> al = new ArrayList<Integer>();

while (input.hasNext() && set.size()<MAX)

{

Integer a = Integer.parseInt(input.next());

// Adding the element

  if(!set.contains(a))

  {

set.add(a);

al.add(a);

}

}

System.out.println(al);

}



}