开发者

Checking one dimensional array for duplicate integers Java

hey guys, 开发者_运维技巧 right i have an array of 50 random integers and i have to check if any of them are the same, here is my code so far it only checks ajacent indexs

for (int i =0; i < 50; i++)
{
    System.out.print("Student" + i + ": "   );

    customers[i] = (int)((Math.random()*10000)%10+1);
    System.out.print(" " +customers[i]+ "\n");



    if( duplicate == customers[i])
    {
        System.out.println("yup");
    }
    duplicate = customers[i];
}


Sort the array first. Then you can check just the next index. If it's ever the same, break.


Okay, I hate ridiculous limitations. If you want, you can do it like this without using a sort:

import java.util.ArrayList;
import java.lang.Math;

public class Main {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<Integer>();
        Integer currentValue = 0;

        int i = 0;
        int limit = 20;

        for(i = 0; i < limit; i++) {
            list.add((int)(Math.random() * 100));            
        }      

        for(i = 0; i < limit; i++) {
            currentValue = list.get(i);
            list.set(i, -1);
            if(list.contains(currentValue)) {
                System.out.println("yup:" + currentValue);
                return;
            } else {
                list.set(i, currentValue);
            }
        }

        System.out.println("No duplicates!");
        return;
    }
}

Is it efficient? No.

Does it work? Yes.


I think, if you have to just use if/for functions, you have to make two loops:

for (int i =0; i < 50; i++)
{
    customers[i] = (int)((Math.random()*10000)%10+1);

    for ( int j = 0; j < i; j++)
    {
        if( customers[j] == customers[i])
        {
            // duplicated entry. do what you want
            System.out.println("yup");
        }
    }
}


You could use Arrays.sort (see more at http://www.exampledepot.com/egs/java.util/coll_SortArray.html) to sort your data, and then check for duplicates.


You use value 0-10 so you can save your value in boolean array and it that way verify if this is duplicate or not:

boolean[] checker = new boolean[11];        
for (int i =0; i < 50; i++) {
    customers[i] = (int)((Math.random()*10000)%10+1);
    if (checker[customers[i]]) {
        System.out.println("yup"); //duplication
    } else {
        checker[customers[i]] = true;
    }
}


You can use the below code if you are working with Integer array:

public class DuplicateInteger {

    private static int countDuplicate;

    public static int[] getDuplicateIntegers(int[] integerArray){
        int duplicateIntegers[] = new int[integerArray.length];
        countDuplicate = 0;
        for(int i=0;i<integerArray.length;i++){
            for(int j=i+1;j<integerArray.length;j++){
                int replicaTest = 0;
                if(integerArray[i]==integerArray[j]){
                    for(int k=0;k<countDuplicate;k++){
                        if(duplicateIntegers[k]==integerArray[i]){
                            replicaTest = 1;
                        }
                    }
                    if(replicaTest==0){
                        duplicateIntegers[countDuplicate] = integerArray[i];
                        countDuplicate++;
                    }
                }
            }
        }
        return duplicateIntegers;
    }

    public static void printDuplicateIntegers(int[] duplicateIntegers){
        System.out.println("Duplicate Integers:");
        System.out.println("-------------------");
        for(int i=0;i<countDuplicate;i++){
            System.out.println(duplicateIntegers[i]);
        }   
    }

    public static void main(String[] args){
        int numberArray[] = {1, 2, 3, 4, 5, 6, 7, 1, 3, 5, 7};
        printDuplicateIntegers(getDuplicateIntegers(numberArray));
    }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜