Home > Net >  How to mark and edit duplicate words in list java?
How to mark and edit duplicate words in list java?

Time:09-02

Hi I have this list below

String[] list = {"I","think","she","think","he","think","she","loves"};

I want to produce it like below. So the repeated words get increment.

 ["I","think","she","think(1)","he","think(2)","she(1)","loves"];

I've tried to explore this logic but I find it hard to add the increment number to my list, so far I'm only able to detect the repeated words. How can I add the number to the repeated words?

CodePudding user response:

You can traverse the array and store each word with their number of occurrences in a Map object. As you traverse through and you find a word which is already present in the map then its simply means its a duplicate word.
EG:

Map<String, Integer> map = new HashMap<>();
String[] result = new String[list.length];
int i = 0;
for (String val : list) {
    int count = map.getOrDefault(val, 0); // if map does not contain the key then the default occurrence is 0     
    result[i] = count > 0 ? val   "("   count   ")" : val;
    count  ;
    map.put(val, count);
    i  ;
}

Edit:
As mentioned by @Holger in the comments , a simplified for-loop.

for(String val : list) {
  int count = map.merge(val, 1, Integer::sum) - 1;
  result[i  ] = count > 0 ? val   "("   count   ")" : val;
}

CodePudding user response:

Here is one possible solution:

 String[] list = {"I", "think", "she", "think", "he", "think", "she", "loves"};
    List<String> modified = new ArrayList<>();
    Map<String, Integer> words = new HashMap<>();
    for (String item : list) {
        int val = words.getOrDefault(item, 0);
        words.put(item, val   1);
    }
    for (Map.Entry<String, Integer> entry : words.entrySet()) {
        int val = entry.getValue();
        String key = entry.getKey();
        while (val > 0) {
            if (val - 1 > 0) {
                String newKey = key   "("   (val - 1)   ")";
                modified.add(newKey);
            } else {
                modified.add(key);
            }

            val--;
        }

    }
    String[] result = modified.toArray(new String[0]);
    System.out.println(Arrays.toString(result));

if you run this code, you should see output like this:

[think(2), think(1), think, she(1), she, loves, I, he]

CodePudding user response:

class Test {
    public static void main(String[] args) {
        String[] list = { "I", "think", "she", "think", "he", "think", "she", "loves" };
        List<String> finalList = new ArrayList<>();
        for (int i = 0; i < list.length; i  ) {
            String elem = list[i];
            long matchedCount = Arrays.asList(list).subList(0, i).parallelStream().filter(a -> a.equals(elem)).count();
            if (matchedCount > 0) {
                finalList.add(elem   "("   matchedCount   ")");
            } else {
                finalList.add(elem);
            }
        }
        System.out.println(finalList);
    }
}

Output: [I, think, she, think(1), he, think(2), she(1), loves]
  • Related