如果数字出现三次,如何避免重复计数重复计数?

时间:2021-04-25 19:56:42
public static int countRepeats(int[] items) {
    int l=items.length;
    int num=0;
    int[] count=new int[l];
    for(int i=0;i<l;i++){
        for(int j=i+1;j<l;j++){
            if(items[i]==items[j]){
                count[i]++;
            }
        }
    }
    for(int i=0;i<l;i++){
        if(count[i]>0){
            num++;
        }
    }
    return num;
}

//{1,2,1,3,4,5,5}should give 2;2numbers repeated //{0,0,0} but my code give 2 for this one..

// {1,2,1,3,4,5,5}应该给2; 2个数字重复// {0,0,0}但是我的代码为这个给出了2个..

4 个解决方案

#1


1  

Your algorithm is not correct. Not sure about your input, but if all numbers are positive and not very big (not big enough to worry about memory), you may try this. It can handle any number of repeats.

你的算法不正确。不确定你的输入,但如果所有数字都是正数且不是很大(不足以担心内存),你可以试试这个。它可以处理任意数量的重复。

public static int countRepeats(int[] items) {
    int l=items.length;
    int num=0;
    int max=0;
    for(int i=0;i<l;i++){
        if(items[i] > max) max = items[i];  // get the largest number
    }
    int[] count=new int[max + 1];  // assume count elements are initiated with 0
    for(int i=0;i<l;i++){
       count[items[i]]++;
    }
    for(int i=0;i<=max;i++){
        if(count[i]>1){
            num++;
        }
    }
    return num;
}

#2


1  

Sets only store distinct items. You can use one set to find duplicates and another to store them distinctly, then return the size of the latter set:

设置仅存储不同的项目。您可以使用一个集合查找重复项,另一个集合可以明确地存储它们,然后返回后一个集合的大小:

public static int countRepeats(int[] items) {
    Set<Integer> distinct = new HashSet<>();
    Set<Integer> duplicate = new HashSet<>();
    for (int item : items) {
        if (!distinct.add(item)) {
            // item was already contained in set
            duplicate.add(item);
        }
    }
    return duplicate.size();
}

#3


1  

From your code, array {0, 0, 0}, the first element will be counted as 2, the second element will be counted as 1, then the last one will be counted as 0, of course, it gives you 2. Try this:

从你的代码,数组{0,0,0},第一个元素将被计为2,第二个元素将被计为1,然后最后一个将被计为0,当然,它给你2.尝试这个:

public static int countRepeats(int[] items) {
    int num = 0;
    Map<Integer, Integer> countMap = new HashMap<Integer, Integer>();
    for (Integer i : items) {
        if (countMap.containsKey(i)) { // check if map does contain the key or not, if does, make this key'value +1;
            countMap.put(i, countMap.get(i) + 1);
        } else { // if not contain the key, just put it as a new key and the value is 1.
            countMap.put(i, 1);
        }
    }
    for (Integer item : countMap.values()) {
        if (item > 1) {
            num++;
        }
    }
    return num;
}

Use a Map to store the number's appear times, then get all the values in this map, count the value which is over 1 then you can get what you want.

使用地图存储数字的出现次数,然后获取此地图中的所有值,计算超过1的值,然后您就可以获得所需的值。

#4


0  

The Map maybe helpful,just like:

地图可能有用,就像:

   public static int countRepeats(int[] items) {
        int res = 0;
        Map<Integer,Integer> map = new HashMap<>();
        for(int i:items){
            map.put(i, map.get(i)==null?0:map.get(i)+1);
        }

        for(Integer key:map.keySet()){
            if(map.get(key)>0){
                res++;
            }
        }
        return res;
    }

#1


1  

Your algorithm is not correct. Not sure about your input, but if all numbers are positive and not very big (not big enough to worry about memory), you may try this. It can handle any number of repeats.

你的算法不正确。不确定你的输入,但如果所有数字都是正数且不是很大(不足以担心内存),你可以试试这个。它可以处理任意数量的重复。

public static int countRepeats(int[] items) {
    int l=items.length;
    int num=0;
    int max=0;
    for(int i=0;i<l;i++){
        if(items[i] > max) max = items[i];  // get the largest number
    }
    int[] count=new int[max + 1];  // assume count elements are initiated with 0
    for(int i=0;i<l;i++){
       count[items[i]]++;
    }
    for(int i=0;i<=max;i++){
        if(count[i]>1){
            num++;
        }
    }
    return num;
}

#2


1  

Sets only store distinct items. You can use one set to find duplicates and another to store them distinctly, then return the size of the latter set:

设置仅存储不同的项目。您可以使用一个集合查找重复项,另一个集合可以明确地存储它们,然后返回后一个集合的大小:

public static int countRepeats(int[] items) {
    Set<Integer> distinct = new HashSet<>();
    Set<Integer> duplicate = new HashSet<>();
    for (int item : items) {
        if (!distinct.add(item)) {
            // item was already contained in set
            duplicate.add(item);
        }
    }
    return duplicate.size();
}

#3


1  

From your code, array {0, 0, 0}, the first element will be counted as 2, the second element will be counted as 1, then the last one will be counted as 0, of course, it gives you 2. Try this:

从你的代码,数组{0,0,0},第一个元素将被计为2,第二个元素将被计为1,然后最后一个将被计为0,当然,它给你2.尝试这个:

public static int countRepeats(int[] items) {
    int num = 0;
    Map<Integer, Integer> countMap = new HashMap<Integer, Integer>();
    for (Integer i : items) {
        if (countMap.containsKey(i)) { // check if map does contain the key or not, if does, make this key'value +1;
            countMap.put(i, countMap.get(i) + 1);
        } else { // if not contain the key, just put it as a new key and the value is 1.
            countMap.put(i, 1);
        }
    }
    for (Integer item : countMap.values()) {
        if (item > 1) {
            num++;
        }
    }
    return num;
}

Use a Map to store the number's appear times, then get all the values in this map, count the value which is over 1 then you can get what you want.

使用地图存储数字的出现次数,然后获取此地图中的所有值,计算超过1的值,然后您就可以获得所需的值。

#4


0  

The Map maybe helpful,just like:

地图可能有用,就像:

   public static int countRepeats(int[] items) {
        int res = 0;
        Map<Integer,Integer> map = new HashMap<>();
        for(int i:items){
            map.put(i, map.get(i)==null?0:map.get(i)+1);
        }

        for(Integer key:map.keySet()){
            if(map.get(key)>0){
                res++;
            }
        }
        return res;
    }