Question

In: Computer Science

Write a Java method that removes any duplicate elements from an ArrayList of integers. The method...

Write a Java method that removes any duplicate elements from an ArrayList of integers. The method has the following header(signature):

public static void removeDuplicate(ArrayList<Integer> list)

Write a test program (with main method) that prompts the user to enter 10 integers to a list and displays the distinct integers separated by exactly one space. Here is what the input and output should look like:

     Enter ten integers: 28 4 2 4 9 8 27 1 1 9

     The distinct integers are 28 4 2 9 8 27 1

Solutions

Expert Solution

// RemoveDuplicates.java
import java.util.Scanner;
import java.util.ArrayList;

public class RemoveDuplicates
{
public static void removeDuplicate(ArrayList<Integer> integerList)
{

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

for (int i = 0; i < integerList.size(); i++)
{
// check for duplicates
if (!temporaryList.contains(integerList.get(i)))
{
// add to temporary list
temporaryList.add(integerList.get(i));
}
}
// clear the integer list
integerList.clear();
// add the temp list to ineter list
integerList.addAll(temporaryList);

}

public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);

int size = 10;
int number;
  
ArrayList<Integer> integerList = new ArrayList<>();

System.out.print("Enter ten integers: ");

for (int i = 0; i < size; i++)
{
number = scan.nextInt();
integerList.add(number);
}

// calling the method
removeDuplicate(integerList);

System.out.println("The distinct integers are " + integerList);
}

}

/*
output:

Enter ten integers: 28 4 2 4 9 8 27 1 1 9
The distinct integers are [28, 4, 2, 9, 8, 27, 1]


*/


Related Solutions

JAVA Write a method that removes the duplicate elements from an array list of integers using...
JAVA Write a method that removes the duplicate elements from an array list of integers using the following header: public static void removeDuplicate(ArrayList list) Write a test program that prompts the user to enter 10 integers to a list and displays the distinct integers separated by exactly one space. Here is a sample run: Enter ten integers: 10 20 30 20 20 30 50 60 100 9 The distinct integers are: [10, 20, 30, 50, 60, 100, 9]
java/ netbeans Write a recursive method smallestNumber which takes an ArrayList of Integers as input and...
java/ netbeans Write a recursive method smallestNumber which takes an ArrayList of Integers as input and returns the smallest number in the array. You can use a helper method if needed. Write a main method that asks the user for a series of numbers, until the user enters a period. Main should create an ArrayList of these Integers and call smallestNumber to find the smallest number and print it. Compile and test your code in NetBeans and then on Hackerrank.
Code in Java Write a recursive method smallestNumber which takes an ArrayList of Integers as input...
Code in Java Write a recursive method smallestNumber which takes an ArrayList of Integers as input and returns the smallest number in the array. You can use a helper method if needed. Write a main method that asks the user for a series of numbers, until the user enters a period. Main should create an ArrayList of these Integers and call smallestNumber to find the smallest number and print it. Input Format A series of integers Constraints None Output Format...
I need to implement a method that removes any duplicate items in a list and returns...
I need to implement a method that removes any duplicate items in a list and returns the new values    private static linkedlist removeDuplicates(linkedlist list) {    return list; }
My add method is not working to add elements into an arrayList in java. This is...
My add method is not working to add elements into an arrayList in java. This is the error message I keep getting: Exception in thread "main" java.lang.NullPointerException    at assignment1.ArrayBag.add(ArrayBag.java:50)    at assignment1.Main.main(Main.java:21) BUILD FAILED (total time: 0 seconds) The following are all the methods I've created. /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package...
An increasing-order integer array may contain duplicate elements. Write a Java method that does a binary...
An increasing-order integer array may contain duplicate elements. Write a Java method that does a binary search through the array for finding a specific target and return an array of two integer indices that specifies the close interval of indices at which the target is found or null if the target is not present. Use this test driver to test public class MultBS { int [] multBinarySearch(int [] x, int target) { // your codes go here } public static...
Java Write a method that removes duplicates from an array of strings and returns a new...
Java Write a method that removes duplicates from an array of strings and returns a new array, free of any duplicate strings.
Java 1.Write a method removeEvenLength that takes an ArrayList of Strings as a parameter and that...
Java 1.Write a method removeEvenLength that takes an ArrayList of Strings as a parameter and that removes all of the strings of even length from the list. 2. Given the following Vehicle interface and client program in the Car class: public interface Vehicle{ public void move(); } public class Car implements Vehicle{ public static void main(String args[]) Vehicle v = new Vehicle(); // vehicle declaration } The above declaration is valid? True or False? 3. Java permits a class to...
Given two ArrayLists of Strings (ArrayList<String>), write a Java method to return the higher count of...
Given two ArrayLists of Strings (ArrayList<String>), write a Java method to return the higher count of the characters in each ArrayList.  For example, if list1 has strings (“cat, “dog”, “boat”, “elephant”) and list 2 has strings (“bat”, “mat”, “port”, “stigma”), you will return the value 18.  The list 1 has 18 characters in total for all its strings combined and list2 has 16 characters for all of its strings combined.  The higher value is 18. If the character count is the same, you...
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array...
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array and remove the duplicates in-place such that each element appears only once in the input array and returns the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Find the time complexity of your removeDuplicates() method in Big-O notation and write that in a comment line on the top...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT