Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ArrayUtil {
- /**
- * Field INPUT_LESS_THAN_TWO. (value is 2)
- */
- private static final int INPUT_LESS_THAN_TWO = 2;
- /**
- * Method removeDuplicateElement. Given a sorted array of integers, this
- * method removes the duplicate elements. Input array is sorted array and
- * can have only negative values, only positive value or a mix of both.
- *
- * @param inputArray
- * int[]
- *
- * @return int[]
- * @throws IllegalArgumentException
- */
- public int[] removeDuplicateElement(final int[] inputArray)
- throws IllegalArgumentException {
- int j = 0; // Marks the begining of the array
- int i = 1; // Points toward the second element in the array
- if (null == inputArray || 0 == inputArray.length) {
- throw new IllegalArgumentException(
- "Input Array is either null or of zero length");
- } // end of null and 0 length check
- // Return if the input array has only one item
- if (inputArray.length < INPUT_LESS_THAN_TWO) {
- return inputArray;
- } // end of check for length less than 2
- while (i < inputArray.length) {
- if (inputArray[i] == inputArray[j]) {
- i++;
- } else {
- inputArray[++j] = inputArray[i++];
- }
- } // end of while loop
- // copy and return the array
- final int[] outputArray = new int[j + 1];
- for (int k = 0; k < outputArray.length; k++) {
- outputArray[k] = inputArray[k];
- }
- return outputArray;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement