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,709 questions

55,473 answers

573 users

How to remove duplicate case‑insensitive words separated by multiple delimiters from a string in Java

1 Answer

0 votes
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Arrays;
import java.util.List;
import java.util.Set;


/**
    Remove duplicate case‑insensitive words separated by multiple delimiters.

    Features:
    - Case‑insensitive comparison (ASCII)
    - Preserves original casing of first occurrence
    - Trims whitespace around tokens
    - Supports ANY number of delimiters, including multi‑character ones
    - Preserves original order
    - Uses LinkedHashSet for O(n) duplicate removal while preserving order

    Algorithm:
    1. Build a regex that matches ANY delimiter.
    2. Replace all delimiters with a single sentinel.
    3. Split by that sentinel.
    4. Trim each token.
    5. Convert to lowercase for comparison.
    6. Keep only first occurrence.
    7. Reassemble using a chosen delimiter.
*/

public class Main {

    // Build a regex that matches ANY delimiter
    private static String buildDelimiterRegex(List<String> delimiters) {
        StringBuilder sb = new StringBuilder();
        sb.append("(");
        for (int i = 0; i < delimiters.size(); i++) {
            if (i > 0) sb.append("|");
            sb.append(java.util.regex.Pattern.quote(delimiters.get(i)));
        }
        sb.append(")");

        return sb.toString();
    }

    public static String removeDuplicatesMultiDelimiterCI(
            String input,
            List<String> delimiters,
            String outputDelimiter) {

        // Step 1: Build regex for all delimiters
        String regex = buildDelimiterRegex(delimiters);

        // Step 2: Replace all delimiters with a sentinel
        String sentinel = "\n";
        String normalized = input.replaceAll(regex, sentinel);

        // Step 3: Split by sentinel
        String[] tokens = normalized.split(sentinel);

        // Step 4: Remove duplicates (case‑insensitive)
        Set<String> seen = new HashSet<>();
        List<String> unique = new ArrayList<>();

        for (String token : tokens) {
            String trimmed = token.trim();
            if (trimmed.isEmpty()) continue;

            String key = trimmed.toLowerCase();

            if (!seen.contains(key)) {
                seen.add(key);
                unique.add(trimmed);
            }
        }

        // Step 5: Reassemble
        return String.join(outputDelimiter, unique);
    }

    public static void main(String[] args) {

        String s = "AAA | aaa ,   aAA * aaA | AAa | AAA   | BBB | ccc ---- CCC | AAA ; aaa | bbb";

        List<String> delimiters = Arrays.asList(
                "  ", "|", ",", "*", "-", ";"
        );

        String result = removeDuplicatesMultiDelimiterCI(s, delimiters, " | ");

        System.out.println(result);
    }
}


/*
run:

AAA | BBB | ccc

*/

 



answered Aug 1 by avibootz

Related questions

...