当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Removing duplicates from an array without set

问题描述:

 EDITED VERSION

I need to remove duplicates from an array for a project. I have seen that people advise using "set", I haven't learned that in my class so I can't use it. I've asked my instructor and he pointed me in a good direction. This is very long but that is only because of the many print statements that I am using to help me understand what the code is doing. (CODE AT BOTTOM). I believe there is a problem with inserting the non duplicate number in the array.

 HERE IS MY OUTPUT

LB: 0

UB: 10

PROBE: 5

Value of Arrays: 0

randomNumber:42

LB: 6

UB: 10

PROBE: 8

Value of Arrays: 0

randomNumber:42

LB: 9

UB: 10

PROBE: 9

Value of Arrays: 0

randomNumber:42

LB: 10

UB: 10

PROBE: 10

Value of Arrays: 0

randomNumber:42

Return False

42

LB: 0

UB: 10

PROBE: 5

Value of Arrays: 42

randomNumber:75

LB: 6

UB: 10

PROBE: 8

Value of Arrays: 42

randomNumber:75

LB: 9

UB: 10

PROBE: 9

Value of Arrays: 42

randomNumber:75

LB: 10

UB: 10

PROBE: 10

Value of Arrays: 0

randomNumber:75

Return False

42

75

42

42

42

42

42

42

42

42

LB: 0

UB: 10

PROBE: 5

Value of Arrays: 75

randomNumber:74

LB: 0

UB: 4

PROBE: 2

Value of Arrays: 75

randomNumber:74

LB: 0

UB: 1

PROBE: 0

Value of Arrays: 42

randomNumber:74

LB: 1

UB: 1

PROBE: 1

Value of Arrays: 75

randomNumber:74

Return False

42

75

74

75

75

75

75

75

75

75

LB: 0

UB: 10

PROBE: 5

Value of Arrays: 75

randomNumber:100

LB: 6

UB: 10

PROBE: 8

Value of Arrays: 75

randomNumber:100

LB: 9

UB: 10

PROBE: 9

Value of Arrays: 75

randomNumber:100

LB: 10

UB: 10

PROBE: 10

Value of Arrays: 0

randomNumber:100

Return False

42

75

75

100

75

75

75

75

75

75

LB: 0

UB: 10

PROBE: 5

Value of Arrays: 100

randomNumber:68

LB: 0

UB: 4

PROBE: 2

Value of Arrays: 75

randomNumber:68

LB: 0

UB: 1

PROBE: 0

Value of Arrays: 42

randomNumber:68

LB: 1

UB: 1

PROBE: 1

Value of Arrays: 75

randomNumber:68

Return False

42

75

75

100

68

100

100

100

100

100

LB: 0

UB: 10

PROBE: 5

Value of Arrays: 100

randomNumber:7

LB: 0

UB: 4

PROBE: 2

Value of Arrays: 75

randomNumber:7

LB: 0

UB: 1

PROBE: 0

Value of Arrays: 42

randomNumber:7

Return False

42

75

75

75

100

7

100

100

100

100

LB: 0

UB: 10

PROBE: 5

Value of Arrays: 100

randomNumber:29

LB: 0

UB: 4

PROBE: 2

Value of Arrays: 75

randomNumber:29

LB: 0

UB: 1

PROBE: 0

Value of Arrays: 42

randomNumber:29

Return False

42

42

75

75

75

100

29

100

100

100

LB: 0

UB: 10

PROBE: 5

Value of Arrays: 75

randomNumber:39

LB: 0

UB: 4

PROBE: 2

Value of Arrays: 42

randomNumber:39

LB: 0

UB: 1

PROBE: 0

Value of Arrays: 42

randomNumber:39

Return False

42

42

42

75

75

75

100

39

100

100

LB: 0

UB: 10

PROBE: 5

Value of Arrays: 75

randomNumber:74

LB: 0

UB: 4

PROBE: 2

Value of Arrays: 42

randomNumber:74

LB: 3

UB: 4

PROBE: 3

Value of Arrays: 42

randomNumber:74

LB: 4

UB: 4

PROBE: 4

Value of Arrays: 75

randomNumber:74

Return False

42

42

42

42

75

75

75

100

74

100

LB: 0

UB: 10

PROBE: 5

Value of Arrays: 75

randomNumber:42

LB: 0

UB: 4

PROBE: 2

Value of Arrays: 42

randomNumber:42

LB: 0

UB: 10

PROBE: 5

Value of Arrays: 75

randomNumber:67

LB: 0

UB: 4

PROBE: 2

Value of Arrays: 42

randomNumber:67

LB: 3

UB: 4

PROBE: 3

Value of Arrays: 42

randomNumber:67

LB: 4

UB: 4

PROBE: 4

Value of Arrays: 75

randomNumber:67

Return False

42

42

42

42

75

75

75

75

100

67

LB: 0

UB: 10

PROBE: 5

Value of Arrays: 75

randomNumber:30

LB: 0

UB: 4

PROBE: 2

Value of Arrays: 42

randomNumber:30

LB: 0

UB: 1

PROBE: 0

Value of Arrays: 42

randomNumber:30

Return False

42

42

42

42

75

75

75

75

75

100

HERE IS MY CODE

public class BinarySearch3{

public static boolean binarySearch(int[] Arrays, int randomNumber){

int LB = 0;//declare the lower bound

int UB = 10;//declare the upper bound

int probe = (LB + UB) / 2;//calculate the probe

while(LB <= UB){

System.out.println("LB: " + LB);

System.out.println("UB: " + UB);

System.out.println("PROBE: " + probe);

System.out.println("Value of Arrays: " + Arrays[probe]);

System.out.println("randomNumber:" + randomNumber);

//if the number is found return true

if(Arrays[probe] == randomNumber)

return true;

// if the probe is less than the number you want to find make LB

// the probe + 1. Cutting the list in half

if(Arrays[probe] < randomNumber)

LB = probe + 1;

// if the probe is more than the number you want to find make UB

// the probe - 1. Cutting the list in half

else if(Arrays[probe] > randomNumber)

UB = probe - 1;

probe = (LB + UB) / 2;//recalculate probe

}

// the number was not found

System.out.println("Return False");

return false;

}

public static void main(String [] args){

int Arrays[] = new int [11];

//Check the array

int randomNumber = 0; int d=0 ;

while(d < Arrays.length){

sort.sorting(Arrays);

do{ // loop until randomNumber not found in array

randomNumber = (int) (Math.random() * 100) + 1;

} while(binarySearch(Arrays, randomNumber) == true);

Arrays[d] = randomNumber;

d++;

for(int k = 0; k < Arrays.length-1; k++) {

System.out.println(Arrays[k]);

}

}

}

}

网友答案:

One equal sign is to assign a value.

this if(dup = false) should be if(!dup) or if(dup == false)

Another thing in your code is that you create the variable boolean test = true; and you never change it. So in the second iteration it will do nothing, because your while statment is while(!test)

You have to improve your code a bit in order to execute properly.

网友答案:

I would suggest making the binary search it's own function, this would separate out the building of the array from the binary search. This make readability and testing much easier and the binary search reusable.

public class work_on_it{
    public static boolean binarySearch(int[] array, int z){
        int LB = 0;//declare the lower bound
        int UB = array.length - 1;//declare the upper bound
        int probe = (LB + UB) / 2;//calculate the probe
        while(LB <= UB){
            //if the number is found return true
            if(array[probe] == z)
                return true;
            // if the probe is less than the number you want to find make LB
            // the probe + 1. Cutting the list in half
            if(array[probe] < z)
                LB = probe + 1;
            // if the probe is more than the number you want to find make UB
            // the probe - 1. Cutting the list in half
            else if(array[probe] > z)
                UB = probe - 1;
            probe = (LB + UB) / 2;//recalculate probe
        }
        // the number was not found
        return false;
    }

    public static void main(String [] args){
        // array length = LENGTH, array values in (1,...,RANGE)
        int LENGTH = 11, RANGE = 50;
        int[] array = new int [LENGTH];
        // fill array with MAX_VALUE. Ensures unassigned elements last after sort.
        Arrays.fill(array, Integer.MAX_VALUE);

        int z = 0, x = 0;
        while(x < array.length){
            Arrays.sort(array);
            do{ // loop until z not found in array
                z = (int) (Math.random() * RANGE) + 1;
            } while(binarySearch(array, z) == true);
            array[x++] = z;  
        }

        for(int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
    }
}  
网友答案:

I have not gone through the code logic completely, but here are a couple of mistakes I found,

1. if(dup = true) { z = (int)(Math.random() * 10) + 1; }
else if(dup = false) { array[x] = z;
}

Over here, you are assigning the values and not comparing, use '==' for comparison.

2. while(!test) Over here, since test is set to true before, it will never go inside the while as the condition will be evaluated to false

分享给朋友:
您可能感兴趣的文章:
随机阅读: