Question

In: Computer Science

(Java Programming Problem) Build two ArrayList lists from the Integer and String type arrays, populate the...

(Java Programming Problem)

Build two ArrayList lists from the Integer and String type arrays, populate the lists with repeats (see example below). Write a generic method (removeDuplicates( ….. )) to remove those duplicate items and returns an ArrayList<E> list without duplicates.

Original Integer List: [14, 24, 14, 42, 24, 25, 25, 23]

No-Duplicate List: [14, 24, 42, 25, 23]

Same generic method for the name list

Original List: [Mike, Lara, Jenny, Lara, Jared, Jonny, Lindsey, Mike, Jared]

No-Duplicate List: [Mike, Lara, Jenny, Jared, Jonny, Lindsey]

Solutions

Expert Solution

SOLUTION-
I have solve the problem in Java code with comments and screenshot for easy understanding :)

CODE-

//java code
import java.util.ArrayList;
import java.util.Arrays;
//class
public class RemoveDuplicates {
//to remove duplicates
public static <E> ArrayList<E> removeDuplicates(ArrayList<E> list) {
ArrayList<E> newList = new ArrayList<>();
for(int i = 0; i < list.size(); ++i)//find for duplicate in array list
   {
if(!newList.contains(list.get(i))) //if two same element is present in array list
{
newList.add(list.get(i));
}
}
return newList; //return new list
}

public static void main(String[] args)
{
ArrayList<Integer> numbers = new ArrayList<>(Arrays.asList(14, 24, 14, 42, 24, 25, 25, 23)); //given array list
System.out.println("original Integer list" + numbers); //print given list
System.out.println("no-dup list " + removeDuplicates(numbers)); //print new list
//given name array list
ArrayList<String> names = new ArrayList<>(Arrays.asList("Mike", "Lara", "Jenny", "Lara", "Jared", "Jonny", "Lindsey", "Mike", "Jared"));
System.out.println("original list" + names); //print given list
System.out.println("no-dup list" + removeDuplicates(names)); //print new list
}
}


SCREENSHOT-


OUTPUT -

IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I WILL SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK YOU!!!!!!!!----------


Related Solutions

JAVA: when input is type ArrayList<ArrayList<Integer>> how to use java to get this solution [ [a,b,c]...
JAVA: when input is type ArrayList<ArrayList<Integer>> how to use java to get this solution [ [a,b,c] , [d,e], [f] ] ----> [ [a,d,f], [a,e,f], [b,d,f], [b,e,f], [c,d,f], [c,e,f]] [ [a,b], [a,b,c]] ----->[[a,a],[a,b],[a,c], [b,a],[b,b],[b,c] assuming abc are integer Thanks in advance!
JAVA PROGRAMMING 1)BuildLists: Write a program to create an ArrayList<Integer>. Fill it with numbers from 1...
JAVA PROGRAMMING 1)BuildLists: Write a program to create an ArrayList<Integer>. Fill it with numbers from 1 to 1000. Then remove every even number. Then remove every multiple of 3 remaining Then remove every multiple of 5 Then remove every multiple of 7 Then sum the array, and print.
JAVA LANGUAGE ArrayList<Integer> al = new ArrayList<Integer>(); HashSet<Integer> hs = new HashSet<Integer>(); HashMap<Integer, Integer> hm =...
JAVA LANGUAGE ArrayList<Integer> al = new ArrayList<Integer>(); HashSet<Integer> hs = new HashSet<Integer>(); HashMap<Integer, Integer> hm = new HashMap<Integer,Integer>(); for (int i= 0; i<2; i++) { al.add(i); al.add(i+1); hs.add(i); hs.add(i+1); hm.put(al.get(i), al.get(i+1)); hm.put(hm.get(i), hm.get(i+1)); }   System.out.println(al); System.out.println(hs); System.out.println(hm); // {key=value}. ---------------------------------- What output is produced by the following code and why?
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"}
Java: Determine the ouotput of this code ArrayList<String>list=new ArrayList<String>();             list.add("Namath");           &
Java: Determine the ouotput of this code ArrayList<String>list=new ArrayList<String>();             list.add("Namath");             list.add("Sauer");             list.add("Maynard");             list.add("Namath");             list.add("Boozer");             list.add("Snell");             list.add("Namath");             list.add("Atkinson");             list.add("Lammonds");             list.add("Dockery");             list.add("Darnold");             list.remove(2);             list.set(2, "Brady");             list.remove(2);             list.set(4,"Unitas");             list.add(1,"Lamomica");             list.add(3,"Hanratty");             list.remove("Namath");             list.remove(list.size()-1);             list.remove(2);             list.set(7, "Riggins");             Iterator iter = list.iterator();           while (iter.hasNext())         {   System.out.print(iter.next() + " ");                         }                     } }
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].
2. Consider these declarations: ArrayList stringList = new ArrayList(); String ch = “ ”; Integer intOb...
2. Consider these declarations: ArrayList stringList = new ArrayList(); String ch = “ ”; Integer intOb = new Integer(5); For the following statements, indicate VALID or INVALID (invalid if the statement causes an error) A. strList.add(ch); _______________ B. strList.add(new String(“handy andy”); ______________ C. strList.add(intOb.toString()); ____________ D. strList.add(ch + 8); _________________ E. strList.add(intOb + 8); ____________
Write Java code that accepts the integer input (from keyboard) in an arraylist named num1 and...
Write Java code that accepts the integer input (from keyboard) in an arraylist named num1 and stores the even integers of num1 in another arraylist named evennum.
Python Programming Problem: If I have to separate lists, one contains a large string of paragraphs...
Python Programming Problem: If I have to separate lists, one contains a large string of paragraphs of words, and one contains just words, how can i iterate the words in my second list and compare it to my paragraph list to see how many times that word has occurred? List1 = ['paragraph.......'] List2 = ['words', 'words', 'words'......] these are just minimal examples how do i approach this problem? apprently numpy helps with processing time for something like this? cuz the...
In Java, what is the advantage of using ArrayList over arrays? Write a program to perform...
In Java, what is the advantage of using ArrayList over arrays? Write a program to perform following operations a. Create a class named Rectangle with two properties “height” and “width”. Create a parameterized constructor to initialize “height” and “width” values. b. Write method area() to calculate and return area of Rectangle. c. Create an ArrayList to store Rectangle objects. d. Create three Rectangle objects of width and height set to (2, 3), (3, 3) and (4, 5) and add them...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT