简体   繁体   English

如何从阵列中删除重复项?

[英]How do you remove duplicates from an array?

I am trying to make is so that in the method populate as the populate array increases it checks the index above if the value is the same. 我正在尝试做的是,以便在填充数组增加的方法中填充它,如果值相同,它会检查上面的索引。 However, I have no idea how to do it without going through each index individually making it super inefficient. 但是,我不知道如何在不逐一检查每个索引的情况下如何做到这一点而使其效率极低。 I tried to make a for loop where the I value is 0 and increases by 1 and inside that for loop is another for loop with the b value is 1 and that also increases by 1. Inside the second for loop I have an if statement and if the I index is equal to the b index the I index is revalued again 我试图制作一个for循环,其中I值为0,然后增加1;在for循环中,另一个for循环,其b值为1,并且也增加了1。在第二个for循环中,我有一个if语句和如果I索引等于b索引,则再次对I索引重估

public class CH7Ass {

    public static void main(String[] args) {
        int user;
        int[] array;
        int checkUserNum;
        int tries=0;
        System.out.println("I bet you can't guess my six numbers");     
        array=populate();
        do{
            tries++;
            user=getUserNum();
            checkUserNum=checkUserNum(user,array);
            array=removeFromArray(array,checkUserNum);
        }while(tries<6);
    }

    public static int[] populate(){
        int[] populate;
        populate= new int [6];
        int random;

        for(int i=0;i<populate.length;i++){
            random=(int) ((Math.random())*50);      //goes from 0-49
            populate[i]=random;
            System.out.println(populate[i]);
            for(int b=1;b<populate.length;b++){
                if(populate[b]==populate[i]){
                    populate[i]=random;
                }
            }
        }
        return  populate;
    }


    public static int getUserNum(){
        int getUserNum;

        do{
            System.out.println("Please input a number from 1 to 49:");
            getUserNum=TextIO.getInt();
        }while((getUserNum<1)||(getUserNum>49));

        return getUserNum;
    }

    public static int checkUserNum(int getUserNum,int[] array){
        boolean check=false;
        int checkUserNum = 0;
        for(int i=0;i<array.length;i++){
            if(array[i]==getUserNum){
                check=true;
                System.out.println("You got it");
                checkUserNum=i;
            }
        }
        if(check==false){
            checkUserNum=-1;
            System.out.println(checkUserNum);
        }

        return checkUserNum;
    }


    private static int[] removeFromArray(int[] array,int checkUserNum){
        int[] removeFromArray;
        if(checkUserNum!=-1){
            array[checkUserNum]=0;
            removeFromArray=array[checkUserNum];
        }
        return removeFromArray;
    }
}

I am by no means an expert in algorithms, but I think one simple and easy to implement modification you could make would be the following. 我绝不是算法专家,但是我认为可以进行以下简单而容易的修改。

At each iteration of your algorithm, instead of starting the inner loop at b = 1 , you could start it at b = i + 1 . 在算法的每次迭代中,您都可以在b = i + 1处启动内部循环,而不是在b = 1处启动内部循环。 This would yield an equivalent result since, if you are at index i of the outer loop, you have already compared all indexes smaller than i with the rest of the array. 这将产生等效的结果,因为如果您位于外部循环的索引i处,您已经将所有小于i的索引与数组的其余部分进行了比较。

You could do it by setting up some min max values that increase as your for loop works. 您可以通过设置一些最小最大值来实现此目的,这些最大值会随着for循环的进行而增加。 That way you set boundaries for the random number and force them to be within different boundaries. 这样,您可以为随机数设置边界,并强制它们位于不同的边界内。

public static int[] populate(){
   int[] populate = new int [6];
   int random = 0;
   double maximum = 0.0;
   for(int i=0;i<populate.length;i++){
      maximum += ( 50 / 6 ); 
      random =   ( Math.random()  *  (  maximum  - (8 * i )  ) +  (8 * i ) ) ;      
      populate[i] = random;
   }
   return  populate;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM