1

I am working on a program that will get user input and then store it in an array. Then, it will search the array to see if the number entered by the user is a duplicate or not. For some reason binarySearch is not detecting duplication.

My code is:

import java.util.Arrays;
import java.util.Scanner;

public class Duplicate_Elimination 
{
   public static void main(String[] args) 
   {

   int [] numbers = new int[5]; // create an array
   Scanner input = new Scanner(System.in); // create a Scanner

       // Ask user for 5 numbers
       for (int i = 0; i < 5; i++)
       {
           // ask user to type the number
           System.out.printf("Please type number %d:",i+1);
           int number = input.nextInt(); // get user input and store it in number variable

           // check if the number entered is already stored
           int location = Arrays.binarySearch(numbers,number);

           // store the value inside number variable in numbers array 
           numbers[i] = number;

           showArray(numbers);

           // if there is any similarity between numbers, print error message
           if (location >= 0)
               System.out.printf("%s%n","error");
       } // end for
   }

   // this method will show what's in the array
   public static void showArray(int[] numbers)
   {
       System.out.printf("%n%n"); // print black spaces

       // print all the numbers stored in the array
       for(int digit: numbers)
       {
           System.out.printf("%d",digit);
       }

       System.out.printf("%n"); // print black space
   }
}

I tried my very best to solve but when ever I type 1 as all my numbers, it only show duplication after typing like 3 numbers. It should detect duplication after each entry. Can you please help me figure out what's wrong with my code. I will really appreciate that.

Thanks!!

3
  • 4
    binary search requires your array is sorted- doesn't look like that is the case Commented Apr 22, 2015 at 0:16
  • Is it a requirement to use an array with binary search? Commented Apr 22, 2015 at 0:20
  • Not really i can use anything. But i didnt find anything else to use so i used Arrays. Commented Apr 22, 2015 at 0:22

2 Answers 2

3

From the java.util.Arrays documentation:

public static int binarySearch(int[] a, int key)

Searches the specified array of ints for the specified value using the binary search algorithm. The array must be sorted (as by the sort(int[]) method) prior to making this call. If it is not sorted, the results are undefined.

So you need to sort the array after every insertion.

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

1 Comment

Thanks for your reply. I was not sorting my array. So I will fix that now :)
0

From what I can see I think taking out the similarity by commenting it out should work.

showArray(numbers);

       // if there is any similarity between numbers, print error message
       //if (location >= 0)
           //System.out.printf("%s%n","error");
   } // end for
 }

Comments

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.