Let's say I have two ArrayList objects. The user will enter some letters, but in this example I will just hardcode some letters to an ArrayList to make life easier. It seems a simple problem but I am getting so confused over it!
ArrayList<String> letters = new ArrayList<String>();
ArrayList<String> duplicateLetters = new ArrayList<String>();
letters.add("z");
letters.add("a");
letters.add("z");
letters.add("z");
letters.add("b");
My aim, is to remove the duplicates from the ArrayList letters
, so it will ultimately only contain ["z"], ["a"] and ["b"]
.
However, I want the ArrayList duplicateLetters
to store any letters which was entered more than once. Because "z"
was duplicated twice, I want duplicateLetters to contain ["z"]
and ["z"]
in this example.
If possible, I would also like to keep track of the amount of times each letter was duplicated.
I have already tried using a HashSet which works very effectively when removing the duplicates in a List (See this question: How do I remove repeated elements from ArrayList?). However, Sets just simply disregard duplicates and in this case I want to keep track of them.
I'm begging for ideas! :/