CC-3 : Remove Duplicates from list and preserve sequence of remaining.
Description:
Given an array containing duplicate elements, remove the duplicates and preserve the sequence of unique elements.
Test cases and expected outputs:
| Input Parameters |
Expected outputs |
| arr[]={ 1.0, 4.6, 8.1, 3.7, 4.60, 1};
|
1.0,4.6,8.1,3.7
|
| arr[]={ 7.2, 8.0, 5,3};
|
7.2,8.0,5.0,3.0
|
Pseudocode:
| The java method should accept following input parameters: arr (double array).
|
| Initialize a variable set of type LinkedHashSet to hold the input elements.
|
| Using a for loop iterate through arr, using idx as a loop variable with initial value 0 and increment idx till it reaches arr.length-1:
Add arr[idx] to set.
|
| At completion of above loop, set contains all the unique elements from arr with input/original order preserved for the remaining unique elements.
|
| Use an iterator to access elements from set and add the same to double array named retVal. Return retVal.
|
Code:
public double[] setArrayRemoveDuplicatesPreserveOrder(double[] arr) {
LinkedHashSet<Double> set=new LinkedHashSet<Double>();
for (int idx=0; idx <arr.length; idx++) {
set.add(arr[idx]);
}
double retVal[]=new double[set.size()];
int idx=0;
Iterator<Double> itr=set.iterator();
while (itr.hasNext()) {
retVal[idx]=itr.next().doubleValue();
idx++;
}
return retVal;
}
Click here to download and run code and test cases !
Below fully running code can be copied and run on Eclipse or other Java IDEs. Refer the classname in code below. If the class name below is "A", save the code below to a file named A.java before running it.
Be sure to try your own test cases to enhance your understanding !
You can also tweak the code to optimize or add enhancements and custom features.
import java.util.Iterator;
import java.util.LinkedHashSet;
public class SetArrayRemoveDuplicatesPreserveOrder {
public double[] setArrayRemoveDuplicatesPreserveOrder(double[] arr) {
LinkedHashSet<Double> set=new LinkedHashSet<Double>();
for (int idx=0; idx <arr.length; idx++) {
set.add(arr[idx]);
}
double retVal[]=new double[set.size()];
int idx=0;
Iterator<Double> itr=set.iterator();
while (itr.hasNext()) {
retVal[idx]=itr.next().doubleValue();
idx++;
}
return retVal;
}
public static void main(String[] args) {
SetArrayRemoveDuplicatesPreserveOrder sp=new SetArrayRemoveDuplicatesPreserveOrder();
double[] retVal;
try {
double arr1[]={ 1.0, 4.6, 8.1, 3.7, 4.60, 1};
printArraySummary(arr1, "Original Array1");
retVal=sp.setArrayRemoveDuplicatesPreserveOrder(arr1);
printArraySummary(retVal, "Common Elements");
double arr2[]={ 7.2, 8.0, 5,3};
printArraySummary(arr2, "Original Array1");
retVal=sp.setArrayRemoveDuplicatesPreserveOrder(arr2);
printArraySummary(retVal, "Common Elements");
}catch (Exception exception) {
System.out.print("Exception,"+ exception);
exception.printStackTrace();
}
}
public static void printArraySummary(double[] intArray, String label) throws Exception {
// Case 1: The input Array is null !!
if (intArray == null) { System.out.println("\n\n Input Array was null !! \n"); return; }
// Case 2: Print input Array by index (first to last)
System.out.println();
System.out.println("************************************************************************");
System.out.print(label+" : ");
int arrayIndex=0;
for (arrayIndex=0; arrayIndex< intArray.length; arrayIndex++) {
System.out.print(intArray[arrayIndex]);
if (arrayIndex< intArray.length-1) {System.out.print(",");}
}
System.out.println();
System.out.println("*************************************************************************");
System.out.println();
Thread.sleep(2000);
return;
}
}