Question

In: Computer Science

Java Programming CS 209 Data Structure 1. Create a method that takes an ArrayList of String...

Java Programming

CS 209 Data Structure

1. Create a method that takes an ArrayList of String and returns a copy of that ArrayList with no duplicates.

The relative ordering of elements in the new ArrayList should be the same.

Sample Input: {"qwerty", "asdfgh", "qwer", "123", "qwerty", "123", "zxcvbn", "asdfgh"}

Sample Output: {"qwerty", "asdfgh", "qwer", "123", "zxcvbn"}

Solutions

Expert Solution

import java.util.*;
public class Main
{
   public static void main(String[] args) {
      
       // Get the ArrayList with duplicate values
ArrayList<String>
list = new ArrayList<>(
Arrays
.asList("qwerty", "asdfgh", "qwer", "123", "qwerty", "123", "zxcvbn", "asdfgh"));
  
// Print the Arraylist
System.out.println("ArrayList with duplicates: "
+ list);
  
// Remove duplicates
ArrayList<String>
newList = removeDuplicates(list);
  
// Print the ArrayList with duplicates removed
System.out.println("ArrayList with duplicates removed: "
+ newList);
}
  
  
   // Function to remove duplicates from an ArrayList
public static <T> ArrayList<T> removeDuplicates(ArrayList<T> list)
{
  
// Create a new ArrayList
ArrayList<T> newList = new ArrayList<T>();
  
// Traverse through the first list
for (T element : list) {
  
// If this element is not present in newList
// then add it
if (!newList.contains(element)) {
  
newList.add(element);
}
}
  
// return the new list
return newList;
}
  
}


Related Solutions

CS 209 Data Structure 1. Create a method that takes an ArrayList of Integer and returns...
CS 209 Data Structure 1. Create a method that takes an ArrayList of Integer and returns a sorted copy of that ArrayList with no duplicates. Sample Input: {5, 7, 4, 6, 5, 6, 9, 7} Sample Output: {4, 5, 6, 7, 9}
CS 209 Data Structure 2. Create a method that takes a HashMap and returns the sum...
CS 209 Data Structure 2. Create a method that takes a HashMap and returns the sum of the keys of the HashMap. 3. Create a method that takes a HashMap and returns the sum of all keys and values of the HashMap. For example, if the input is [1=9, 3=6, 4=9, 6=8, 7=6] then the method should return 59.
I am trying to create a method in JAVA that takes in an ArrayList and sorts...
I am trying to create a method in JAVA that takes in an ArrayList and sorts it by the requested "amenities" that a property has. So if someone wants a "pool" and "gym" it would show all members of the array that contain a "pool" and "gym". It does not need to output the values in anyway, but it should return them so they can be output elsewhere. Please try to use the stub class below. You can edit it...
I am trying to create a method in JAVA that takes in an ArrayList<Property> and filters...
I am trying to create a method in JAVA that takes in an ArrayList<Property> and filters it by the requested price range that a property has. So if someone wants a property between the value of 10(min) and 20(max) it would show all members of the array that meet those conditions.. It does not need to output the values in anyway, but it should return them so they can be output elsewhere. Please try to use the stub class below....
I am trying to create a method in JAVA that takes in an ArrayList<Property> and sorts...
I am trying to create a method in JAVA that takes in an ArrayList<Property> and sorts it by the amount of "reviews" that a property has in increasing order. So the most reviews first. So each listing in the array would contain a different number of reviews, and they should be sorted based on that value. It does not need to output the values in anyway, but it should return them so they can be output elsewhere. Please try to...
CS 209 Data Structure 3. a. Create a class named Point3D that contains 3 instance variables...
CS 209 Data Structure 3. a. Create a class named Point3D that contains 3 instance variables x, y, and z. b. Create a constructor that sets the variables. Also, create get and set methods for each variable. c. Create a toString() method. d. Make Point3D implement Comparable. Also, create a compareTo(Point3D other) method that compares based on the x-coordinate, then y-coordinate for tiebreakers, then z-coordinate for tiebreakers. For example, (1, 2, 5) comes before (2, 1, 4), which comes before...
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...
I am trying to create a function in JAVA that takes in an ArrayList and sorts...
I am trying to create a function in JAVA that takes in an ArrayList and sorts the values by their distance in miles in increasing order. So the closest (or lowest) value would be first. It does not need to output the values in anyway, but it should return them so they can be output elsewhere. Please try to use the stub class below. The code for the main class is not necessary. I am only really asking for the...
CS 209 Data Structure 1. show how to apply a selection sort on {45, 11, 50,...
CS 209 Data Structure 1. show how to apply a selection sort on {45, 11, 50, 59, 60, 2, 4, 7, 10}. 2. show how to apply a Insertion sort on {45, 11, 50, 59, 60, 2, 4, 7, 10}. 3. show how to apply a Bubble sort on {45, 11, 50, 59, 60, 2, 4, 7, 10}. 4. show how to apply a Merge sort on {45, 11, 50, 59, 60, 2, 4, 7, 10}. 5. show how to...
CS 209 Data Structure 5. Consider the Pair class covered in class: class Pair {    ...
CS 209 Data Structure 5. Consider the Pair class covered in class: class Pair {     public A first;     public B second;     public Pair(A a, B b)     {         first = a;         second = b;     }     public void setFirst(A a)     {         first = a;     }     public A getFirst()     {         return first;     }     public void setSecond(B b)     {         second = b;     }     public...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT