如何计算Java中数组中的第一个重复值

时间:2015-04-12 12:58:22

标签: java

我已经采用数组int[] a = {33,33,5,5,9,8,9,9};在这个数组中,所以很多值都是重复的,意味着33次来了两次& 5也来了两次& 9次来了三次。 但是我想计算第一个值,即重复意味着33是第一个值,它是两次,所以答案是2。

我试试:

public class FindFirstDuplicate
{

public static void main(String[] args) {

    int c=0;
    int[] a = {33,33,5,5,9,8,9,9};

    outerloop:
    for(int i = 0; i < a.length; i++)
    {
        for(int j = i+1; j< a.length; j++)
        {
            if(a[i] == a[j])
            {
                System.out.println(a[i]); //Duplicate value
                c++;
                break outerloop;
            }
        }

    }
    System.out.print("Count: "+c);

    }
}

输出: 33 1

5 个答案:

答案 0 :(得分:1)

尝试类似:

int[] numbers = {33, 33, 5, 5, 9, 8, 9, 9};
Set<Integer> set = new HashSet<Integer>();
for (int i = 0; i< numbers.length; i++) {
   if (!set.add(number[i])) {
        System.out.println("first duplicate is " + number[i] + " and index is " + i);
        break;
    }
 }

答案 1 :(得分:1)

public class HelloWorld{
    public static void main(String[] args) {
        int[] a = {33,33,5,5,9,8,9,9};
        for(int i = 0; i < a.length; i++)
        {
            int c=1; // we already found one. 
                     // and we initialize this counter inside the loop, 
                     // so that it is reset for each new starting number.
            for(int j = i+1; j< a.length; j++) // we're starting from next number (reason we start with c=1)
            {
                if(a[i] == a[j])
                    c++;

            }
            if(c > 0) {
                System.out.println("First uplicate value: "+ a[i] + " Count: " + c);
                break;      // we have to break out of the outer loop, 
                            // so the inner loop can finish counting duplicates
            }
        }
    }
}

答案 2 :(得分:0)

如果数组中的值非负且相当小,您可以使用BitSet存储以前是否看到过值:

BitSet bits = new BitSet();
for (int i = 0; i < numbers.length; ++i) {
  if (bits.get(numbers[i])) {
    System.out.println("first duplicate at " + i + ": " + numbers[i]);
    break;
  }
  bits.set(numbers[i]);
}

答案 3 :(得分:0)

你可以尝试一下:

int[] a = {33,33,5,5,9,8,9,9};
Integer[] uniques = new Integer[a.length];
Integer[] counts = new Integer[a.length];
int len = 0;
for(int num : a){
   boolean matched = false;
   for(int i = 0; i < len; i++){
      if(num == uniques[i].intValue()){
         matched = true;
         counts[i] = new Integer(counts[i]+1);
         break;
      }
   }
   if(!matched){
      uniques[len] = new Integer(num);
      counts[i] = new Integer(1);
      len++;
   }
}
for(int i = 0; i < len; i++){
   if(counts[i].intValue() > 1){
      System.out.println("first duplicate is " + uniques[i] + " and number of times it appears " + counts[i]);
      break;
   }
}

答案 4 :(得分:0)

在您的代码中,在找到第一个副本后退出两个循环,因此将忽略该元素的任何其他出现。

此外,您从c = 0开始。当您第二次出现时,c将会增加并为1,而不是2

要计算所有元素,只需更改外部循环的循环条件,然后删除break

int c = 1;
int i;
for(i = 0; (c == 1) && (i < a.length); i++)
{
    for(int j = i+1; j < a.length; j++)
    {
        if(a[i] == a[j])
        {
            c++;
        }
    }

}

System.out.println(a[i]); //Duplicate value
System.out.print("Count: "+c); // maybe do something else, if c == 1 (no duplicates)???

然而SMA's answer描述了找到第一个副本的更高效的方式(对于任意输入数组)。一旦找到第一个副本的第二个出现,您只需要计算数组其余部分中出现的次数以获得最终计数。