如何删除问题

问题描述 投票:-5回答:2

我有三个数组,第一个数组充满了我需要寻找的单词

d
2个回答
0
投票

创建哈希图

HashMap<String,Integer> hmap = new HashMap<String, Integer>() 

并循环遍历第一个数组,并将值存储在此HashMap中,因为我们知道HashMap中的键不能有重复项,当遇到重复项时,将值的计数更新为1。

现在检查entry.getValue = 1,这将使您所有唯一存储到唯一[]数组中

以及其中entry.getValue> = 1将其存储到频率[]数组中


0
投票

最简单易用的解决方案是,对单词数组进行两次扫描以查找唯一性,然后第二次对频率进行计数。下面的代码对数组进行两次双重扫描,并在唯一数组和频率数组的相同索引上填充唯一字和频率。

public static void main(String[] args) {
    String [] words = {"one", "two", "three", "four",
                       "two", "five", "six", "six",
                       "seven", "eight", "four", "nine", "ten"};

    String [] uniquewords = new String[words.length];
    int [] frequency = new int[words.length];

    //First scan to find uniques
    for (int i=0; i<words.length; i++){
        String toMatch = words[i];
        boolean isUnique = true;
        for (int j=0; j<words.length; j++){
            if (i==j){
                //do not match with itself
                continue;
            }
            if (words[j].equals(toMatch)){
                //Found duplicate
                isUnique = false;
                break;
            }
        }
        if (isUnique){
            uniquewords[i] = toMatch;
        }
    }
    System.out.println("Print Uniques with Index");
    for (int i=0; i<uniquewords.length; i++){
        System.out.println(i + ": " + uniquewords[i]);
    }
    //Second scan to count frequencies
    for (int i=0; i<words.length; i++) {
        String toMatch = words[i];
        int freq = 0;
        for (int j = 0; j < words.length; j++) {
            if (words[j].equals(toMatch)) {
                //Found duplicate
                freq++;
            }
        }
        frequency[i] = freq;
    }
    System.out.println("Print Frequency with Index and word");
    for (int i=0; i<frequency.length; i++){
        System.out.println(i + ": " + frequency[i] + " - " + words[i]);
    }
}

以上'main()'方法的输出是

Print Uniques with Index
0: one
1: null
2: three
3: null
4: null
5: five
6: null
7: null
8: seven
9: eight
10: null
11: nine
12: ten
Print Frequency with Index and word
0: 1 - one
1: 2 - two
2: 1 - three
3: 2 - four
4: 2 - two
5: 1 - five
6: 2 - six
7: 2 - six
8: 1 - seven
9: 1 - eight
10: 2 - four
11: 1 - nine
12: 1 - ten

uniquewords数组的大小与words数组相同,并且保留相同的索引来存储唯一的单词。如果在该索引中找到null,则表示单词数组的该索引处的单词不包含唯一值。

类似地,frequency双重扫描数组,以便在word数组的相同索引处找到每个单词的频率。因此,如果在3个索引中找到单词test,则频率“ 3”将放置在频率数组的3个索引中。

您可以优化上面的代码,以便通过查找唯一性并通过将两个循环替换为同时计数频率来完成一次双重扫描

for (int i=0; i<words.length; i++){
            String toMatch = words[i];
            boolean isUnique = true;
            int freq = 0;
            for (int j=0; j<words.length; j++){
                if (i==j){
                    freq++;
                    //do not match with itself
                    continue;
                }
                if (words[j].equals(toMatch)){
                    //Found duplicate
                    isUnique = false;
                    freq++;
                }
            }
            if (isUnique){
                uniquewords[i] = toMatch;
            }
            frequency[i] = freq;
        }

这样,代码进行了双重扫描,并立即计算出唯一性和频率。

可以进行更多的优化,但是我知道您的问题只针对数组和简单的实现。

© www.soinside.com 2019 - 2024. All rights reserved.