How to use Binary Search to find duplicates in sorted array?

imparante

I am attempting to expand a function to find the number of integer matches through Binary Search by resetting the high variable, but it gets stuck in a loop. I am guessing a workaround would be to duplicate this function to obtain the last index to determine the number of matches, but I do not think this would be such an elegant solution.

From this:

public static Matches findMatches(int[] values, int query) {
    int firstMatchIndex = -1;
    int lastMatchIndex = -1;
    int numberOfMatches = 0;

    int low = 0;
    int mid = 0;
    int high = values[values.length - 1];
    boolean searchFirst = false;

    while (low <= high){
        mid = (low + high)/2;

        if (values[mid] == query && firstMatchIndex == -1){
            firstMatchIndex = mid;

            if (searchFirst){
                high = mid - 1;
                searchFirst = false;
            } else { 
                low = mid + 1;
            }

        } else if (query < values[mid]){
            high = mid - 1;
        } else {
            low = mid + 1;
        }           
    }

    if (firstMatchIndex != -1) { // First match index is set
        return new Matches(firstMatchIndex, numberOfMatches);
    }
    else { // First match index is not set
        return new Matches(-1, 0); 
    }
}

To something like this:

public static Matches findMatches(int[] values, int query) {
    int firstMatchIndex = -1;
    int lastMatchIndex = -1;
    int numberOfMatches = 0;

    int low = 0;
    int mid = 0;
    int high = values[values.length - 1];
    boolean searchFirst = false;

    while (low <= high){
        mid = (low + high)/2;

        if (values[mid] == query && firstMatchIndex == -1){
            firstMatchIndex = mid;

            if (searchFirst){
                high = values[values.length - 1]; // This is stuck in a loop
                searchFirst = false;
            } 
        } else if (values[mid] == query && lastMatchIndex == -1){
            lastMatchIndex = mid;

            if (!searchFirst){
                high = mid - 1;
            } else { 
                low = mid + 1;
            }
        } else if (query < values[mid]){
            high = mid - 1;
        } else {
            low = mid + 1;
        }

    }

    if (firstMatchIndex != -1) { // First match index is set
        return new Matches(firstMatchIndex, numberOfMatches);
    }
    else { // First match index is not set
        return new Matches(-1, 0); 
    }
}
Sumeet

There is a problem with your code:

high = values[values.length - 1];

should be

high = values.length - 1;

Also you do not need variables like numberOfMatches and searchFirst, we can have rather simple solution.

Now coming to the problem,I understand what you want I think Binary Search is appropriate for such query.

The Best way to do the required is once a match is found you just go forward and backward from that index until a mismatch occurs and this would be both elegant and efficient in calculating the firstMatchIndex and numberOfMatches.

So your function should be:

public static Matches findMatches(int[] values, int query) 
{
 int firstMatchIndex = -1,lastMatchIndex=-1;
 int low = 0,mid = 0,high = values.length - 1;
 while (low <= high)
 {
      mid = (low + high)/2;

      if(values[mid]==query)
      {
          lastMatchIndex=mid;
          firstMatchIndex=mid;
          while(lastMatchIndex+1<values.length&&values[lastMatchIndex+1]==query)
           lastMatchIndex++;
          while(firstMatchIndex-1>=0&&values[firstMatchIndex-1]==query)
           firstMatchIndex--; 
          return new Matches(firstMatchIndex,lastMatchIndex-firstMatchIndex+1); 
      }
      else if(values[mid]>query)
       high=mid-1;
      else low=mid+1;
 }
 return new Matches(-1,0);
}          

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Find a number in sorted multidimentional array with binary search

From Dev

Check sorted non sequential array for duplicates using binary search?

From Dev

Can we use binary search to find most frequently occuring integer in sorted array?

From Dev

Binary Search Like Algorithm to Find Change in Value in Sorted Array

From Dev

Using binary search tree to find duplicates

From Dev

Using binary search tree to find duplicates

From Dev

Insert sorted array into binary search tree

From Dev

binary search of sorted array in assembly language

From Dev

Insert sorted array into binary search tree

From Dev

Binary search inside sorted array in OCaml

From Dev

How to allow duplicates in a binary search tree?

From Dev

how to use return keyword in a find operation in binary search tree

From Dev

how to use return keyword in a find operation in binary search tree

From Dev

How to find a leaf node in a binary NOT SORTED tree

From Dev

Binary search to find last element in sorted list that is less then specific value

From Dev

Binary Search Tree - Sorted?

From Dev

Binary Search Tree - Sorted?

From Dev

Duplicates in a sorted java array

From Dev

Convert (un)sorted array to binary search tree in c

From Dev

Parallelize Convert Sorted Array to Binary Search Tree Function

From Dev

Parallelize Convert Sorted Array to Binary Search Tree Function

From Dev

1 Dimensional sorted Array: KD-Tree vs Binary Search

From Dev

Return the insert position of an element into a sorted array with Binary Search Recursion

From Dev

Find largest number of not sorted array without using linear and binary

From Dev

How to retrieve elements from sorted TreeSet using Binary Search?

From Dev

how to apply binary search in python on sorted list of string elements?

From Dev

Binary search for sorted multiple items

From Dev

Binary search in many sorted arrays

From Dev

Binary search in a sorted text file

Related Related

  1. 1

    Find a number in sorted multidimentional array with binary search

  2. 2

    Check sorted non sequential array for duplicates using binary search?

  3. 3

    Can we use binary search to find most frequently occuring integer in sorted array?

  4. 4

    Binary Search Like Algorithm to Find Change in Value in Sorted Array

  5. 5

    Using binary search tree to find duplicates

  6. 6

    Using binary search tree to find duplicates

  7. 7

    Insert sorted array into binary search tree

  8. 8

    binary search of sorted array in assembly language

  9. 9

    Insert sorted array into binary search tree

  10. 10

    Binary search inside sorted array in OCaml

  11. 11

    How to allow duplicates in a binary search tree?

  12. 12

    how to use return keyword in a find operation in binary search tree

  13. 13

    how to use return keyword in a find operation in binary search tree

  14. 14

    How to find a leaf node in a binary NOT SORTED tree

  15. 15

    Binary search to find last element in sorted list that is less then specific value

  16. 16

    Binary Search Tree - Sorted?

  17. 17

    Binary Search Tree - Sorted?

  18. 18

    Duplicates in a sorted java array

  19. 19

    Convert (un)sorted array to binary search tree in c

  20. 20

    Parallelize Convert Sorted Array to Binary Search Tree Function

  21. 21

    Parallelize Convert Sorted Array to Binary Search Tree Function

  22. 22

    1 Dimensional sorted Array: KD-Tree vs Binary Search

  23. 23

    Return the insert position of an element into a sorted array with Binary Search Recursion

  24. 24

    Find largest number of not sorted array without using linear and binary

  25. 25

    How to retrieve elements from sorted TreeSet using Binary Search?

  26. 26

    how to apply binary search in python on sorted list of string elements?

  27. 27

    Binary search for sorted multiple items

  28. 28

    Binary search in many sorted arrays

  29. 29

    Binary search in a sorted text file

HotTag

Archive