根据需要使用 ArrayUtil 类。除了删除重复项之外,我还编写了一些方法。此类是在不使用任何 Collection 框架类的情况下实现的。
public class ArrayUtils {
/**
* Removes all duplicate elements from an array.
* @param arr Array from which duplicate elements are to be removed.
* @param removeAllDuplicates true if remove all duplicate values, false otherwise
* @return Array of unique elements.
*/
public static int[] removeDuplicate(int[] arr, boolean removeAllDuplicates) {
int size = arr.length;
for (int i = 0; i < size;) {
boolean flag = false;
for (int j = i + 1; j < size;) {
if (arr[i] == arr[j]) {
flag = true;
shrinkArray(arr, j, size);
size--;
} else
j++;
}
if (flag && removeAllDuplicates) {
shrinkArray(arr, i, size);
size--;
} else
i++;
}
int unique[] = new int[size];
for (int i = 0; i < size; i++)
unique[i] = arr[i];
return unique;
}
/**
* Removes duplicate elements from an array.
* @param arr Array from which duplicate elements are to be removed.
* @return Array of unique elements.
*/
public static int[] removeDuplicate(int[] arr) {
return removeDuplicate(arr, false);
}
private static void shrinkArray(int[] arr, int pos, int size) {
for (int i = pos; i < size - 1; i++) {
arr[i] = arr[i + 1];
}
}
/**
* Displays the array.
* @param arr The array to be displayed.
*/
public static void displayArray(int arr[]) {
System.out.println("\n\nThe Array Is:-\n");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + "\t");
}
}
/**
* Initializes the array with a given value.
* @param arr The array to be initialized.
* @param withValue The value with which the array is to be initialized.
*/
public static void initializeArray(int[] arr, int withValue) {
for (int i = 0; i < arr.length; i++) {
arr[i] = withValue;
}
}
/**
* Checks whether an element is there in the array.
* @param arr The array in which the element is to be found.
* @param element The element that is to be found.
* @return True if found false otherwise
*/
public static boolean contains(int arr[], int element) {
for(int i=0; i< arr.length; i++) {
if(arr[i] == element)
return true;
}
return false;
}
/**
* Removes a element from an array.
* @param arr The array from which the element is to removed.
* @param element The element to be removed
* @return The size of the array after removing.
*/
public static int removeElement(int[] arr, int element) {
int size = arr.length;
for(int i=0; i< arr.length; i++){
if(arr[i] == element){
shrinkArray(arr, i, arr.length);
size--;
}
}
return size;
}
/**
* Counts unique elements in an array.
* @param arr The required array.
* @return Unique element count.
*/
public static int uniqueElementCount(int arr[]) {
int count = 0;
int uniqueCount=0;
int[] consideredElements = new int[arr.length];
initializeArray(consideredElements, 0);
for(int i=0;i<arr.length;i++) {
int element = arr[i];
for(int j=i+1;j<arr.length; j++){
if(element != arr[j] && !contains(consideredElements, element)){
consideredElements[count++] = element;
}
}
}
for(int i=0;i< consideredElements.length;i++)
if(consideredElements[i]!=0)
uniqueCount++;
return uniqueCount;
}
}