0

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];
}
1
  • You have custom values between 1 and 10, and 50 such values, and you want to check if any of the 50 values are duplicated? Ever heard of the pigeon hole principle? Commented Mar 19, 2011 at 23:11

5 Answers 5

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

sorry forgot to mention im not allowed use a sort, just an if, im stumped, i assume somehow ill have to use another array?
@Andrew restrictions like that makes it sound like homework. I tagged it as such, you can change it back if I am wrong.
ha it WAS homework but i missed the submit time now its just for myself
I've edited it with an approach that works without sorts... although it's more "lol" than practical.
1

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");
        }
    }
}

1 Comment

Shouldn't that be j < i? Customer[i+1] is not initialized yet, and it will compare every customer with itself.
0

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.

2 Comments

If you are not allowed to use sort, you could iterate over the array of data to check for duplicates.
as in nest the for with another one?
0

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;
    }
}

Comments

-1

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));
    }

}

1 Comment

This algorithm's performance is O(n^3), which is not very good. See this question for much more information on more elegant / efficient solutions:stackoverflow.com/questions/3951547/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.