You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi!
I found a mistake in method removeDuplicates. Inside the for loops there is an if branch. This contains the following: list.remove(j);
It is wrong, because if you have the same items several times one after another, it will erase only the first one, but not the 2nd, because as you delete the first, the loop will increase value of j, and the 2nd one will not deleted. If you try it with a sample list like this: Chicago, Miami, Chicago, Chicago, Washington, the result will be: Chicago, Miami, Chicago, Washington.
You can correct it, if you decrease 'j' every time you delete an item from the list: list.remove(j--);
Bye,
Csaba
Good catch.
I went ahead and fixed it in this pull request, but after looking at the exercise description, I noticed the "returns a new ArrayList" part and your method doesn't do this because it does modify the ArrayList that's passed in (Java passes parameters by reference).
My version does return a new ArrayList leaving the original alone
It did get a bit longer, but that's to be expected since I am using 2 ArrayLists
It correctly handles empty input lists & won't crash
/**************************************************************************************
* (Distinct elements in ArrayList) Write the following method that returns a new *
* ArrayList. The new list contains the non-duplicate elements from the original list. *
* *
* public static <E> ArrayList<E> removeDuplicates(ArrayList<E> list) *
**************************************************************************************/
import java.util.ArrayList;
public class Exercise_19_03 {
/** Removes duplicate elements from an array list */
public static <E extends Comparable<E>> ArrayList<E> removeDuplicates(ArrayList<E> list) {
ArrayList<E> newList = new ArrayList<E>();
if(!list.isEmpty()){
newList.add(list.get(0)); //adds 1st item from reference list
for (int i = 1; i < list.size(); i++) { //loop starts from 1
boolean shouldAddToNewList = true;
for (int j = 0; j < newList.size(); j++) {
if (list.get(i).compareTo(newList.get(j)) == 0){
shouldAddToNewList = false;
}
}
if(shouldAddToNewList){
newList.add(list.get(i));
}
}
}
return newList;
}
}
Right.
But it would be better to use the contain method of ArrayList in the if branch inside the for loop to check wether the newList contains the element or not. In this case you will need only 1 for loop and not necessary to use a boolean variable.
I considered that, but wasn't sure the real point of the exercise. Using the built-in contains() would be fine, but it's hard to tell sometimes how much the exercise wants you to use existing language features or implement your own.
I'm not sure of the specific implementation of contains() in Java, but I think my version is likely similar
Activity
SleekPanther commentedon Jul 8, 2017
Good catch.
I went ahead and fixed it in this pull request, but after looking at the exercise description, I noticed the "returns a new ArrayList" part and your method doesn't do this because it does modify the ArrayList that's passed in (Java passes parameters by reference).
csv77 commentedon Jul 9, 2017
Right.
But it would be better to use the contain method of ArrayList in the if branch inside the for loop to check wether the newList contains the element or not. In this case you will need only 1 for loop and not necessary to use a boolean variable.
SleekPanther commentedon Jul 9, 2017
I considered that, but wasn't sure the real point of the exercise. Using the built-in
contains()would be fine, but it's hard to tell sometimes how much the exercise wants you to use existing language features or implement your own.I'm not sure of the specific implementation of
contains()in Java, but I think my version is likely similar