patternjavaMinor
Removing duplicates in an array
Viewed 0 times
arrayduplicatesremoving
Problem
Problem: Remove duplicates from a given array of integers.
I want this method to work without using data sets except an array.
My code is currently \$O(n^2 + n) = O(n^2)\$. How do I shorten this? My main goal in writing this code was in the mindset that I am in an interview and that I am given only 20 minutes to solve this problem. Thus, I tried to find the quickest way of solving the problem. Advice on how better to approach these problems would also be very helpful!
I want this method to work without using data sets except an array.
My code is currently \$O(n^2 + n) = O(n^2)\$. How do I shorten this? My main goal in writing this code was in the mindset that I am in an interview and that I am given only 20 minutes to solve this problem. Thus, I tried to find the quickest way of solving the problem. Advice on how better to approach these problems would also be very helpful!
public int[] removeDuplicate(int[] arr)
{
int temp[] = new int[arr.length];
boolean[] check = new boolean[arr.length];
int index = 0;
for(int i = 0; i < arr.length; i++)
{
if(check[i] != true)
temp[index++] = arr[i];
else
continue;
int var = arr[i];
for(int j = i+1; j < arr.length; j++)
{
if(var == arr[j])
check[j] = true;
}
}
int returnArray[] = new int[index];
for(int i = 0; i < returnArray.length; i++)
returnArray[i] = temp[i];
return returnArray;
}Solution
Problem: Remove duplicates from a given array of integers.
An important part of programming interviews is asking clarifying questions,
and try to not assume anything.
For example:
Depending on the answers, you may have a very different problem to solve.
For example if the range of values is limited to
My gut reaction is to assume that the values are in the full range of valid integers, the array is not sorted, and there may be any number of duplicate values. However, during an interview it's important to not assume anything. Whether the candidate asks clarifying questions or just quietly makes assumptions can make a big difference.
(How many times do programmers think they know what system they are supposed to build but they don't really know? Lots of times!)
My main goal in writing this code was in the mindset that I am in an interview and that I am given only 20 minutes to solve this problem.
It's usually ok to make something work the easiest possible way and then gradually make it better. In this example the easiest solution seems to be to iterate into a set and then convert that to an array.
Code review
Using the boolean literals
Use boolean expressions directly:
This is manual array copy:
It's recommended to use
In this example you can replace the above lines with this one:
An important part of programming interviews is asking clarifying questions,
and try to not assume anything.
For example:
- What kind of values are we talking about?
- What is the range of possible values?
- Is the array sorted?
- How many values are there?
- How many duplicate values are there?
- Do you need to preserve the order of the elements that are not removed?
Depending on the answers, you may have a very different problem to solve.
For example if the range of values is limited to
[1:100], or if the array is sorted.My gut reaction is to assume that the values are in the full range of valid integers, the array is not sorted, and there may be any number of duplicate values. However, during an interview it's important to not assume anything. Whether the candidate asks clarifying questions or just quietly makes assumptions can make a big difference.
(How many times do programmers think they know what system they are supposed to build but they don't really know? Lots of times!)
My main goal in writing this code was in the mindset that I am in an interview and that I am given only 20 minutes to solve this problem.
It's usually ok to make something work the easiest possible way and then gradually make it better. In this example the easiest solution seems to be to iterate into a set and then convert that to an array.
public int[] removeDuplicate(int[] arr) {
return toIntArray(toSet(arr));
}
private Set toSet(int[] arr) {
Set set = new LinkedHashSet<>();
for (int num : arr) {
set.add(num);
}
return set;
}
private int[] toIntArray(Set set) {
int[] result = new int[set.size()];
int i = 0;
for (int num : set) {
result[i++] = num;
}
return result;
}Code review
Using the boolean literals
true and false in conditions like this can be a red flag:if (check[i] != true)Use boolean expressions directly:
if (!check[i])This is manual array copy:
int returnArray[] = new int[index];
for(int i = 0; i < returnArray.length; i++)
returnArray[i] = temp[i];
return returnArray;It's recommended to use
System.arraycopy or Arrays.copyOf instead.In this example you can replace the above lines with this one:
return Arrays.copyOf(temp, index);temp is a very poor name. Almost anything would be better, how about unique. With index renamed to uniqueCount, you end up with something quite natural:return Arrays.copyOf(unique, uniqueCount);Code Snippets
public int[] removeDuplicate(int[] arr) {
return toIntArray(toSet(arr));
}
private Set<Integer> toSet(int[] arr) {
Set<Integer> set = new LinkedHashSet<>();
for (int num : arr) {
set.add(num);
}
return set;
}
private int[] toIntArray(Set<Integer> set) {
int[] result = new int[set.size()];
int i = 0;
for (int num : set) {
result[i++] = num;
}
return result;
}if (check[i] != true)if (!check[i])int returnArray[] = new int[index];
for(int i = 0; i < returnArray.length; i++)
returnArray[i] = temp[i];
return returnArray;return Arrays.copyOf(temp, index);Context
StackExchange Code Review Q#123426, answer score: 5
Revisions (0)
No revisions yet.