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

Java removeDuplicates Write a method called removeDuplicates that takes a list o

ID: 3896634 • Letter: J

Question

Java

removeDuplicates

Write a method called removeDuplicates that takes a list of Point objects and that removes duplicates from the list, returning a set of the Point objects from the list. For example, if a list called points contains the following values:

[[x=1,y=3], [x=7,y=7], [x=1,y=3], [x=7,y=8], [x=1,y=3], [x=7,y=7], [x=7,y=8], [x=4,y=2], [x=7,y=8], [x=7,y=8], [x=4,y=2], [x= 4,y=2]]

then the call removeDuplicates(points) should leave the list storing:

and the set returned should store the same values. Your method should preserve the order of values in the list (removing duplicates that come later in the list), but the values in the set can appear in any order.

You may construct iterators and the set to be returned, but you are not allowed to construct other structured objects (no string, set, list, etc.).

Recall the point class has the following methods:

Point(x, y) constructs a point at position (x, y)
Point() constructs a point at position (0, 0)
getX() returns the x coordinate of the point
getY() returns the y coordinate of the point
equals(other) returns true if the points have the
same (x, y), false otherwise   
toString() returns a string in the form "(x, y)"

Explanation / Answer

Solution:

code:

public static void removeDuplicates(ArrayList<String> array) { //This is the method to remove the duplicates from the list
for(int i = 0; i < array.size(); i++)
{
//if any duplicate is there
if(i != array.lastIndexOf(array.get(i)))
{
array.remove(i); //calling the remove function
i--;
}
}
}   ?

I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)

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