Array Based Coding Interview Questions
------------------------------------------------------------
1)How to Remove Duplicates from Array Without Using Java Collection.
------------------------------------------------------------
1)How to Remove Duplicates from Array Without Using Java Collection.
This is a coding question recently asked to one of Java Technical interview. Question was to remove duplicates from an integer array without using any collection classes like Set or LinkedHashSet, which can make this task trivial.
In this post ,I am giving easy solution.
________________________________________________________________
public class ArrayInterviewTest{
public static void main(String args[]) {
int[][] array = new int[][]{
{6, 6, 2, 2, 3, 4, 5},
{6, 6, 6, 6, 6, 6, 6},
{6, 2, 3, 4, 5, 1, 7},
{6, 2, 6, 6, 6, 6, 6},};
for (int[] input : array ) {
System.out.println("Array with Duplicates : " + Arrays.toString(input));
System.out.println("After removing duplicates : " + Arrays.toString(removeDuplicates
(input)));
}
}
/*
* Method to remove duplicates from array in Java, without using
* Collection classes e.g. Set or ArrayList. Algorithm for this
* method is simple, it first sort the array and then compare adjacent
* objects, leaving out duplicates, which is already in the result.
*/
public static int[] removeDuplicates(int[] numbersWithDuplicates) {
// Sorting array to bring duplicates together
Arrays.sort(numbersWithDuplicates);
int[] result = new int[numbersWithDuplicates.length];
int previous = numbersWithDuplicates[0];
result[0] = previous;
for (int i = 1; i < numbersWithDuplicates.length; i++) {
int ch = numbersWithDuplicates[i];
if (previous != ch) {
result[i] = ch;
}
previous = ch;
}
return result;
}
}
________________________________________________________________________________
public static void main(String args[]) {
int[][] array = new int[][]{
{6, 6, 2, 2, 3, 4, 5},
{6, 6, 6, 6, 6, 6, 6},
{6, 2, 3, 4, 5, 1, 7},
{6, 2, 6, 6, 6, 6, 6},};
for (int[] input : array ) {
System.out.println("Array with Duplicates : " + Arrays.toString(input));
System.out.println("After removing duplicates : " + Arrays.toString(removeDuplicates
(input)));
}
}
/*
* Method to remove duplicates from array in Java, without using
* Collection classes e.g. Set or ArrayList. Algorithm for this
* method is simple, it first sort the array and then compare adjacent
* objects, leaving out duplicates, which is already in the result.
*/
public static int[] removeDuplicates(int[] numbersWithDuplicates) {
// Sorting array to bring duplicates together
Arrays.sort(numbersWithDuplicates);
int[] result = new int[numbersWithDuplicates.length];
int previous = numbersWithDuplicates[0];
result[0] = previous;
for (int i = 1; i < numbersWithDuplicates.length; i++) {
int ch = numbersWithDuplicates[i];
if (previous != ch) {
result[i] = ch;
}
previous = ch;
}
return result;
}
}
________________________________________________________________________________
Output :
---------------------------------------------------
Array with Duplicates : [6, 6, 2, 2, 3, 4, 5]
After removing duplicates : [2, 0, 3, 4, 5, 6, 0]
Array with Duplicates : [6, 6, 6, 6, 6, 6, 6]
After removing duplicates : [6, 0, 0, 0, 0, 0, 0]
Array with Duplicates : [6, 2, 3, 4, 5, 1, 7]
After removing duplicates : [1, 2, 3, 4, 5, 6, 7]
Array with Duplicates : [6, 2, 6, 6, 6, 6, 6]
After removing duplicates : [2, 6, 0, 0, 0, 0, 0]
---------------------------------------------------------------