Quick actions

cmd+k|ctrl+k

Navigation

Languages

Find Duplicates

Snippet info

Language

Java

Visibility

public

Author

hospino11

Created

2025-02-11T01:05:30.090148Z

Updated

2025-02-11T01:05:30.090148Z

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

class Main {
    public static void main(String[] args) {
        String arr[] = {"abcd", "java", "dcba", "ajav", "xyz", "epam", "pame", "aepm"};
        
        //Stream.of(arr).map(s -> s.split("")).sorted().map(String::joining);
    
        Map<String, List<String>> occurrences = new HashMap<>();
        
        int length = arr.length;
        for (int i = 0; i < length - 1; i++) {
            String currentWord = arr[i];
            String[] currentWordArray = currentWord.split("");

            String currentWordSorted = Stream.of(currentWordArray).sorted().collect(Collectors.joining());
            
            if (occurrences.containsKey(currentWordSorted)) {
                List<String> wordOcurrences = occurrences.get(currentWordSorted);
                wordOcurrences.add(currentWord);
            } else {
                List<String> newOccurrences = new ArrayList<>();
                newOccurrences.add(currentWord);
                occurrences.put(currentWordSorted, newOccurrences);
            }
        }
        
        System.out.println("Ocurrences found:" + occurrences);
    }
}
INFO