How do you count the number of distinct Strings in an ArrayList without using the Set data structure in the Java libraries?
I made two ArrayLists, one stored and one empty and want to store the empty one with distinct Strings. What am I doing wrongly?
public void distinctCount (WordStream words) {
ArrayList<String> loaded = new ArrayList<String>();
ArrayList<String> empty = new ArrayList<String>();
int count = 0;
// Fill loaded with word stream
for(String i : words) {
loaded.add(i);
}
// Fill empty with loaded
// Catch collisions
for(int i = 0; i < loaded.size(); i++) {
if(loaded.get(i) != empty.get(i)) {
empty.add(loaded.get(i));
}
}
return empty.size();
}
Set" restriction? You could add all elements to aSetthen provide aListview of it. Also, "program to an interface"; preferList<String> empty = new ArrayList<String>();.ArrayListfor a duplicate before each addition.loaded.get(i) != empty.get(i)use!empty.contains(loaded.get(i))