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

Dedup is pronounced \"dee-dupe\" and it referers to the elimination of duplicate

ID: 3776330 • Letter: D

Question

Dedup is pronounced "dee-dupe" and it referers to the elimination of duplicate items (i.e. to de-duplicate them). You are to create an object that will read in a list of integers and then tell you how many of a given number are present in the list. Also, you can ask for an ArrayList of all of the unique numbers in the list.

By way of an example, if you read in a sequence that is 1,1,3,9,5,5,1 and you ask how many "1"s you'll get back "3" as your answer. Also, an ArrayList of unique items will contain 1,3,5,9 (note that they are in ascending order).

The object to implement this is referred to as Dedup and consists of the following public methods:

public static ArrayList<Integer> readData(String fileName) throws IOException - This method takes a filename and reads the data from the file and returns them as an ArrayList. See the file "small.txt" for details on the file format.

public static int howMany(ArrayList<Integer> data, int number) - This will return the number of times number appears in the list data.

public static ArrayList<Integer> returnUnique() - Returns an ArrayList consisting of each unique number in the file specified when readData was called. The numbers must be in ascending order.

Hints

The Arrays object has a sort method but it only works on arrays not ArrayLists.

You will probably need to know how to convert an ArrayList to an Array for this assignment.

Your Dedup.java should contain code to test your Dedup object. Load multiple files and check to make sure that the values match the expected values. Use looping to load and test your object.

Explanation / Answer

//Tested on Eclipse

/************************Dedup.java*****************/

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Scanner;

public class Dedup {
   public static ArrayList<Integer> uniqueList = new ArrayList<>();

   /**
   * In this method we are reading file and adding numbers into list if File
   * not present then it will throw an exception
   *
   * @return arrayList
   */
   public static ArrayList<Integer> readData(String fileName) throws IOException {
       ArrayList<Integer> numbers = new ArrayList<>();
       File file = new File(fileName);
       Scanner scanner = new Scanner(file);
       /**
       * Reading file line by line and splitting by comma(,) and then adding
       * numbers into list
       */
       while (scanner.hasNext()) {

           String line = scanner.nextLine();
           String numberArr[] = line.split(",");
           for (int i = 0; i < numberArr.length; i++) {
               numbers.add(Integer.parseInt(numberArr[i]));
           }

       }
       /**
       * getting unique values by HashSet then inserting into arraylist
       */
       HashSet<Integer> uniqueValues = new HashSet<>(numbers);
       for (Integer value : uniqueValues) {
           uniqueList.add(value);
       }
       Collections.sort(uniqueList);
       return numbers;
   }

   /**
   * we are iterating arraylist of integer checking with each number of list
   * with number if both are same then we are increasing count to 1
   */
   public static int howMany(ArrayList<Integer> data, int number) {
       int count = 0;
       for (int temp : data) {
           if (temp == number) {
               count++;
           }
       }
       return count;
   }

   /**
   * @return arraylist of unique number
   */

   public static ArrayList<Integer> returnUnique() {

       return uniqueList;
   }

   public static void main(String[] args) {

       Scanner input = new Scanner(System.in);

       /**
       * calling readData method using class
       */
       try {
           /**
           * please provide the file path according to you
           */
           ArrayList<Integer> numbers = Dedup.readData("/home/lalchand/chegg/AnshuChegg/src/small.txt");
           System.out.println("Please enter the number, you wana see the counts");
           int number = input.nextInt();
           int cnt = Dedup.howMany(numbers, number);
           System.out.println("count is: " + cnt);
           /**
           * calling returnUnique method to get unique numbers
           */
           numbers = Dedup.returnUnique();
           System.out.println("Unique list of Numbers");
           for (int temp : numbers) {
               System.out.print(temp + " ");
           }
           System.out.println();
       } catch (IOException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }

   }

}


/********************output***************/

Please enter the number, you wana see the counts
1
count is: 3
Unique list of Numbers
1 3 5 9


/******************small.txt*****************/

1,1,3,9,5,5,1

Thanks a lot

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote