1

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! :/

Community
  • 1
  • 1
rednaxela
  • 661
  • 2
  • 9
  • 21

4 Answers4

2

Why not maintain a Map that maps letters to frequencies. Each time the user enters a letter, you would update this map:

Map<String, Integer> map = new HashMap<String, Integer>();

if (map.containsKey(input)) {
    map.put(input, map.get(input) + 1);
} else {
    map.put(input, 1);
}

To create letters, you would then have:

List<String> letters = new ArrayList<String>(map.keySet());

To create duplicateLetters you could loop over the keys and only add those with a value greater than 1:

List<String> duplicateLetters = new ArrayList<String>();

for (String key : map.keySet())
    if (map.get(key) > 1)
        duplicateLetters.add(key);
arshajii
  • 127,459
  • 24
  • 238
  • 287
0

This will work, I'm not sure it's very efficient but I think it is fine. It moves all letters to either newLetters or duplicateLetters.

public static void main(String[] args) {
    ArrayList<String> letters = new ArrayList<String>();
    HashMap<String, Integer> duplicateLetters = new HashMap<String, Integer>();
    ArrayList<String> newLetters = new ArrayList<String>();

    letters.add("z");
    letters.add("a");
    letters.add("z");
    letters.add("z");
    letters.add("b");

    Iterator<String> iterator = letters.iterator();
    while (iterator.hasNext()) {
        String next = iterator.next();
        if (newLetters.contains(next)) {
            Integer value = duplicateLetters.get(next);
            if (value == null) {
                duplicateLetters.put(next, 1);
            }
            else {
                duplicateLetters.put(next, value + 1);
            }
        }
        else {
            newLetters.add(next);
        }
    }

    letters = newLetters;

    for (String letter : letters) {
        System.out.println("these are left: " + letter);
    }

    for (Map.Entry<String, Integer> entry : duplicateLetters.entrySet()) {
        System.out.println(entry.getKey() + " was duplicated " + entry.getValue()+ " time(s)");
    }
}
Andreas Wederbrand
  • 38,065
  • 11
  • 68
  • 78
0

Your question is pretty simple. why don't you use collections algorithm provided in java collections api.

So here is what you need to do :

ArrayList letters = new ArrayList(); // is the list where you need to add elements

//Assuming that you want to maintain a Map implementation to keep track of duplicates as :
Map dupLetters = new HashMap();

//So write your own add method as follows :

public void addLetter(String letter) { int count = Collections.frequency(letters,letter);

if(count >0)

{

 int letterFreq = 0; 

 //This entry is a duplicate so don't add this one to list; put it in the map

if(dupLetters.containsKey(letter))

{ 

    letterFreq = dupLetters.get(letter); 

}

  dupLetters.put(letter,(letterFreq+1))

}

//Hope this will help you !!

GKP
  • 1,057
  • 1
  • 11
  • 24
0

The simple solution to your problem. Better use hashmap to get both repeated string with their total count in arraylist:

public static void main(String args[])

{

  ArrayList<String> al=new ArrayList<String>();
  HashMap<String,Integer> ht=new HashMap<String,Integer>();
  al.add("a");
  al.add("z");
  al.add("z");
  al.add("b"); 
  al.add("a");
  al.add("t");
  al.add("t");
  int ct=0;
    for(String i : al)
    {
        for(String j : al)
           {
            if(i.equals(j))
             {
                ct++;
            }
        }
        if(ct>1)
        {
            ht.put(i, ct);
        } ct=0;
    }

    for(Entry g:ht.entrySet())
    {
        System.out.println(g.getKey()+" "+g.getValue());
    }
}
Manish
  • 59
  • 2
  • 8