如何删除字符串中所有重复的单词,即使没有重复的单词仍然存在?

时间:2018-04-14 19:35:15

标签: java arrays string

示例输入

"Hello hi hi stackoverflow remain only Hello " 

输出:

"stackoverflow remain only"

这是我试过的代码

public static void main(String[] args) throws Exception {
    String name = "Hello hi stackoverflow remain only Hello hi";
    String ar[] = name.split("\\s");
    ArrayList<String> dup = new ArrayList<String>();//duplicate words
    ArrayList<String> res = new ArrayList<String>();
    for (int i = 0; i < ar.length; i++) {
        res.add(ar[i]);
        String del = ar[i];
        for (int j = i + 1; j < ar.length; j++) {
            if (ar[j].equals(dup)) {
                dup.add(del);
                break;
            }
        }
    }
}

for (int i = 0; i < dup.size(); i++) {
    for (int j = i + 1; j < res.size(); j++) {
        if (st[i].equals(st2[j])) {
            res.remove(res.IndexOf(j));
        }
    }
}

5 个答案:

答案 0 :(得分:2)

如果您使用的是Java 8,可以使用removeIfCollection.frequency,如下所示:

String text = "Hello hi hi stackoverflow remain only Hello ";
List<String> list = new ArrayList<>(Arrays.asList(text.split("\\s+")));
list.removeIf(element -> Collections.frequency(list, element) > 1);

我们的想法是删除频率大于1的所有元素。

输出

[stackoverflow, remain, only]

获取一个你可以使用的字符串:

text = String.join(" ", list);// "stackoverflow remain only"

答案 1 :(得分:2)

您的代码看起来太复杂了。相反,使用Java-8流库,您可以:

List<String> result = 
         Pattern.compile("\\s")
                .splitAsStream(name)
                .collect(Collectors.groupingBy(e -> e,
                        LinkedHashMap::new,
                        Collectors.counting()))
                .entrySet()
                .stream()
                .filter(e -> e.getValue() == 1)
                .map(Map.Entry::getKey)
                .collect(Collectors.toList());

或者如果您希望接收器类型为String,则可以使用joining收集器。

String result = 
         Pattern.compile("\\s")
                .splitAsStream(name)
                .collect(Collectors.groupingBy(e -> e,
                        LinkedHashMap::new,
                        Collectors.counting()))
                .entrySet()
                .stream()
                .filter(e -> e.getValue() == 1)
                .map(Map.Entry::getKey)
                .collect(Collectors.joining(" "));

答案 2 :(得分:1)

试试这个

Stream.of(str.split(" "))
            .collect(Collectors.groupingBy(Function.identity(), Collectors.summingInt(n -> 1)))
            .entrySet()
            .stream()
            .filter(e->e.getValue() == 1)
            .map(e->e.getKey())
            .collect(Collectors.joining(" "))

或者像这样

Set<String> set = new HashSet<>();
List<String> orginal = new ArrayList<>(Arrays.asList(as));
orginal.removeAll(Arrays.stream(as)
                  .filter(str -> !set.add(str))
                  .collect(Collectors.toList()));

答案 3 :(得分:1)

与@ YCF_L的答案略有不同的变体,尽可能短路。

List<String> list = new ArrayList<>(Arrays.asList(text.split("\\s")));
list.removeIf(element -> list.stream()
                             .filter(e -> e.equals(element)) 
                             .limit(2) // short circuit
                             .count() > 1);

<强>更新

上面的代码是有效的,但是如果你担心从列表中流式传输,那么更安全的选择就是:

List<String> result = 
      list.stream()
          .filter(element -> list.stream().filter(e -> e.equals(element)).limit(2)
                                 .count() == 1) 
          .collect(Collectors.toList());

答案 4 :(得分:0)

尝试这种方法

    String statement = "Hello hi stackoverflow remain only Hello hi";
    String res = "";
    for (String word : Arrays.asList(statement.split(" "))) {
        String remain = statement.replaceFirst(word, "");
        if(remain.contains(word)) {
            remain=statement.replaceAll(word, "");
        }else {
            res+=word+" ";
        }
    }
    System.out.println(res);