Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,683 questions

55,435 answers

573 users

How to remove duplicate words with Unicode characters from free‑text in Java

1 Answer

0 votes
/**
    Unicode duplicate remover for free text.
    - Uses Java's built-in Unicode support
    - Removes punctuation
    - Lowercases only for comparison
    - Preserves original casing
    - First occurrence wins
    - LinkedHashMap keeps insertion order
*/

import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.util.LinkedHashMap;
import java.util.Map;

public class RemoveUnicodeDuplicates {

    // Remove punctuation while keeping original casing
    static String cleanPreserveCase(String s) {
        StringBuilder out = new StringBuilder();
        s.codePoints().forEach(codePoint -> {
            if (Character.isLetterOrDigit(codePoint)) {
                out.appendCodePoint(codePoint);
            }
        });
        
        return out.toString();
    }

    // Normalize a word: remove punctuation + lowercase (for comparison)
    static String normalizeWord(String s) {
        StringBuilder out = new StringBuilder();
        s.codePoints().forEach(codePoint -> {
            if (Character.isLetterOrDigit(codePoint)) {
                out.appendCodePoint(Character.toLowerCase(codePoint));
            }
        });
        
        return out.toString();
    }

    public static void main(String[] args) throws Exception {
        // Force System.out to print using UTF-8 encoding
        System.setOut(new PrintStream(System.out, true, StandardCharsets.UTF_8.name()));

        String input =
            "Hello! こんにちは,  ,hello こんにちは Bună ziua; Γεια σας Bună ziua *HELLO* Γεια σας";

        Map<String, String> unique = new LinkedHashMap<>();

        for (String word : input.split("\\s+")) {

            String normalized = normalizeWord(word);
            String cleaned    = cleanPreserveCase(word);

            if (!normalized.isEmpty() && !unique.containsKey(normalized)) {
                unique.put(normalized, cleaned);
            }
        }

        StringBuilder out = new StringBuilder();
        for (String original : unique.values()) {
            if (out.length() > 0) {
                out.append(" ");
            }
            out.append(original);
        }

        System.out.println(out.toString());
    }
}


/*
run:

Hello こんにちは Bună ziua Γεια σας

*/

 



answered Aug 3 by avibootz

Related questions

...