javaduplicatesmultisetbag

Data structure for counting occurrences


Say I have an array

[bob, alice, jeff, bob, bob]

I would like to transform this array into

[bob, alice, jeff, bob1, bob2]

That is, determining occurrences.

// iterate through array
// check if element is in finalArray
     // if not add to finalArray
// if found in final array
    // add in duplicateArray
   // check instances of element in duplicateArray and add that number to element
   // insert element+updated number to finalArray

This is my implementation of the above algorithm

     ArrayList<String> list = new ArrayList<>();
    list.add("Bob");
    list.add("Jane");
    list.add("Smith");
    list.add("Bob");
    list.add("Bob");
    list.add("Jane");
    list.add("Smithery");

    ArrayList<String> finalList = new ArrayList<>();
    ArrayList<String> tempList = new ArrayList<>();

    for(String name : list) {
        if(finalList.contains(name)) {
            int count = 0;
            tempList.add(name);
            for(int i=0; i < tempList.size(); i++) {
                if(tempList.get(i) == name) {
                    count++;
                }
            }
            String tempName = name + count;
            finalList.add(tempName);
        } else {
            finalList.add(name);
        }
    }
    for(String name: finalList) {
        System.out.println(name);
    }

My question is although ArrayList, and other data structure have .contains method, do any data structures have methods that return the number of instances of the element in the data structure?


Solution

  • List<String> data = ...;
    List<String> finalData = new ArrayList<>();
    Map<String, Integer> counts = new HashMap<>();
    
    for(String s : data) {
        counts.merge(s, 1, Integer::sum);  // Keep track of occurences seen
    
        int occurences = counts.get(s);  // Get how many there are for s
    
        if(occurences == 1) { 
            finalData.add(s);
        }
        else {  // if more than 1, change the string
            finalData.add(s + (occurences - 1));
        }
    }
    

    Now the real question is, what happens when you have "Bob" in the List twice, and "Bob1" is also in the original list...