Question

In: Computer Science

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}

Solutions

Expert Solution

Implementation in JAVA;

I have tried to explain through several methods:

or I implemented sorting and removing duplicates from ArrayList through Several Methods

just have a look on whole code:

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public class Remove_Duplicate_and_sort_list {

  
  
//   will sort elements in list by using Bubble sort
   public static ArrayList<Integer> sort(ArrayList<Integer> list){
      
       for (int i = 0; i < list.size(); i++) {

       for (int j = list.size() - 1; j > i; j--) {
       if (list.get(i) > list.get(j)) {

       int tmp = list.get(i);
       list.set(i,list.get(j));
       list.set(j,tmp);

       }

       }

       }
      
       return list;
   }
  
  
//   this is easiest method to sort a list by using collections
   public static ArrayList<Integer> sort_directlt(ArrayList<Integer> list){
      
       Collections.sort(list);
      
       return list;
   }
  
  
  
//   this method is valid for remove duplicates from sorted lists only
   public static ArrayList<Integer> removeduplicates(ArrayList<Integer> list){
      
   for(int i=1;i<list.size();i++) {
           if(list.get(i)==list.get(i-1)) {
               list.remove(i-1);
           }
       }
      
       return list;
      
   }
  

  
//   very easiest method to remove duplicates from arrayList by using Set
   public static ArrayList<Integer> removeduplicates_directly(ArrayList<Integer> list){
      
       Set<Integer> hashsetList = new HashSet<Integer>(list);
      
       return (ArrayList<Integer>) hashsetList;
      
   }
  
  
//   one another method for removing duplicates and will valid for sorted and unsorted all lists
   public static ArrayList<Integer> removeduplicates_method2(ArrayList<Integer> al){
      
       for(int i=0;i<al.size();i++){
             
           for(int j=i+1;j<al.size();j++){
           if(al.get(i).equals(al.get(j))){
           al.remove(j);
           j--;
           }
           }
             
           }
      
       return al;
      
   }
  
//   driver method and main method
public static void main(String[] args) {
      
   ArrayList<Integer> list = new ArrayList<Integer> ();
     
   list.add(5);
   list.add(7);
   list.add(4);
   list.add(6);
   list.add(5);
   list.add(6);
   list.add(9);
   list.add(7);
     
   System.out.println("The input list is : "+list);
     
   list=sort(list);
   list=removeduplicates(list);
     
   System.out.println("After sorting and remove Duplicates list is : "+list);
      
}
  
  

}

SAMPLE OUTPUT:

If you have any doubt regarding this question please ask me in comments

// THANK YOU:-)


Related Solutions

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"}
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.
Method: ArrayList<Integer> diff(ArrayList<Integer> list1, ArrayList<Integer> list2) diff() method accepts two ArrayLists of Integer and returns the...
Method: ArrayList<Integer> diff(ArrayList<Integer> list1, ArrayList<Integer> list2) diff() method accepts two ArrayLists of Integer and returns the union of elements in two lists. For example: list1 contains elements [1, 2, 3, 4, 5] list2 contains elements [3, 4, 5, 6, 7] Diff() method should return an array list with elements [1, 2, 3, 4, 5, 6, 7].
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...
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...
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...
CS 209 Data Structure (Postfix notation) Postfix notation is a way of writing expressions without using...
CS 209 Data Structure (Postfix notation) Postfix notation is a way of writing expressions without using parentheses. For example, the expression (1 + 2) * 3 would be written as 1 2 + 3 *. A postfix expression is evaluated using a stack. Scan a postfix expression from left to right. A variable or constant is pushed into the stack. When an operator is encountered, apply the operator with the top two operands in the stack and replace the two...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT