重复消除

时间:2009-11-20 12:03:12

标签: java

我对这个问题有疑问

重复消除 使用一维数组来解决以下问题:编写一个输入10个整数的应用程序。读取每个数字时,只有在它不是已读取的数字的副本时才显示它。使用尽可能小的数组来解决此问题。在用户输入所有值后显示输入的完整唯一值集。

示例输出:

输入10个整数:

12 33 67 9 10 6 6 34 10 19

独特价值观:

12 33 67 9 10 6 34 19 注意问题要求重新打印数组,但没有任何重复的数字

这是我的代码

import java.util.Scanner;

public class duplicate 
{

public static void main(String[] args)
{
    Scanner input = new Scanner(System.in);
    int[] array = new int[10];
    int[] barray = new int[10];

    System.out.println(" Enter 10 integers: ");
    int i ;
    for(i=0;i<array.length;i++)
    {
        array[i]= input.nextInt();
        barray[i] = array[i];
    }
    for(i=0;i<array.length;i++)
    {
        System.out.printf("\t %d ",array[i]);

    }
    System.out.println("\n Unique values are: ");


        for ( i = 0; i < array.length; i++ )
        {


            {
                if ( array[ i ] == barray[ i ] )
                    break;
                  System.out.printf("\t %d",array[i]);

            }


        }

    }

}

8 个答案:

答案 0 :(得分:4)

你真的需要使用数组吗? Set在这里是理想的:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    Set<Integer> set = new HashSet<Integer>(10);

    System.out.println(" Enter 10 integers: ");

    for (int i = 0; i < 10; i++) {
        set.add(input.nextInt());
    }
    System.out.println("Unique values are: ");

    for (Integer i : set) {
        System.out.printf("\t %d", i);
    }
}

答案 1 :(得分:1)

假设您的问题是“打印重复”部分不起作用:您可以使用a Vector吗?如果是这样,您可以在打印唯一值时执行以下操作:

for each item in the array
   if the vector does not contain the item
      print the value
      insert the item in the vector

答案 2 :(得分:1)

您是否考虑过您的问题而不关注代码?想象一下,我给了你一些纸,并要求你写下我喊出来的所有数字,没有重复,然后在我完成时读回我的数字。想一想在编写代码之前如何在现实世界中解决这个问题。例如。你会记下两次数字然后将它们拿出来吗?你需要2张纸 - 一张是重复的,一张没有?

答案 3 :(得分:0)

对于你的最后一个循环,我会考虑这样的事情:

for(int i = 0; i < array.length; i++)
{
    Boolean duplicated = false;
    for(int j = 0; j<i; j++) {
       if(array[i] == array[j]) {
         //number already printed
         duplicated = true; 
         break;
       }
    }
    if(duplicated == false) //print number here.
}

这不是最好的方法,但它遵循你已经做过的事情。
最好的方法是使用适当的结构,我不是Java用户,但可能HashSet是可行的。

答案 4 :(得分:0)

Arrays类http://java.sun.com/javase/6/docs/api/java/util/Arrays.html为这个主题提供了一些有用的方法,比如sort。

  1. 排序数组
  2. 将第一个整数复制到新的输出数组
  3. 遍历所有整数
  4. 如果前一个整数与当前整数不同
  5. 创建输出数组大小为+ 1的新数组并复制所有值 最后添加最后一个值
  6. 跳过输入,循环可能如下所示:

        int[] out = new int[1];     
        int[] ints = new int[]{3,56,2,98,76,4,9,2,3,55};
        Arrays.sort(ints);
        out[0] = ints[0];                                   //handle first value
        for(int i = 1; i < ints.length; i++){           
            if(ints[i-1] != ints[i]){               
                int[] temp = new int[out.length+1];
                for(int j = 0; j < temp.length-1; j++){
                    temp[j] = out[j];                       //copy all previous
                }               
                temp[temp.length - 1] = ints[i];            //add last value
                out = temp;
            }
        }
        System.out.println(Arrays.toString(out));
    

答案 5 :(得分:0)

如何使用BitSet,因为所有值都是整数。

BitSet bs = new BitSet();

for(int i=0; i<array.lenght; i++)
{
    bs.set(array[i]);    
}

System.out.println("Unique Values are: "+bs.toString());

答案 6 :(得分:0)

要改变两件事:

  1. 仅使用单个数组
  2. 在输入上过滤重复项,而不是输出。
  3. 如果为每个输入查找重复数据循环遍历数组。复制?然后不插入,不重复?添加到数组。

    使用可能的最小数组是一个技巧,因为它取决于知道你可能没有提前输入多少重复。有两种解决方案,使用的数组仅对分配中的唯一数字足够大,或者当您超出数组大小时,创建一个新数组并将所有值复制到该数组。赋值是否意味着您只能使用一个恰好是单维的数组实例,或者只使用您使用的数组必须是一维的但您可以使用多个数组?

答案 7 :(得分:0)

你也可以尝试更短的,如果更加模糊......

System.out.println("Unique values "+
    new LinkedHashSet<String>(Arrays.asList(scanner.nextLine().split(" +"))));