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
0
0
0
0
0
0
0
0
0
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]);
}
}
}
}
if(dup = false)should beif(!dup)orif(dup == false)