## United HealthGroup Interview Question for Tech Leads

• 0

Country: India
Interview Type: Written Test

Comment hidden because of low score. Click to expand.
2
of 2 vote

If it is not a sorted Array, we will use merge sort, which will take o(log N) time.
Now take the sorted array, and copy into new array inthis fashion, which takes o(n) time.
int copiedArray[] = new int[10];
void avoidDup(int arr[])
{
int k=0;
for(int i=1;i<arr.length;i++)
{
if(arr[i-1] == arr[i])
continue;
else
copiedArray[k++] = arr[i-1]
}
}

Comment hidden because of low score. Click to expand.
0

Hi I am very new on this site,
Here is the My java solution, could anyone one of you please look into this and let me know if there is any space or complexity issues. Thanks in advance.

``````public int[] removesDuplicate(){
int arr[] = {1,2,7, 9, 9, 5, 4, 9};
int result[]= new int [arr.length];
int k=0;
boolean status = true;
for(int i=0;i<arr.length;i++){
status = true;
for(int j=0;j<k;j++){
if(arr[i]==result[j]){
status = false;
break;
}
}

if(status){
result[k] = arr[i];
k++;
}
}

for(int i=0;i<result.length;i++){
System.out.println(result[i]);
}
return result;``````

Comment hidden because of low score. Click to expand.
0

output not coming what to do??????????????????????????

Comment hidden because of low score. Click to expand.
1
of 1 vote

Pick some sort algorithm based on the more information about the element of the array. some radix sort may be used since those are numbers and there is a extra space available to contain the result. Shrink the final sorted array to remove the duplicated one using two ptrs, one writer, one reader.

Comment hidden because of low score. Click to expand.
1
of 1 vote

Here is a draft of solution using simple hash funtion.

``````def remove_duplicates(input):
if not isinstance(input, list):
else:
min = 0
max = 0
for value in input:
if value < min:
min = value
if value > max:
max = value

COLLISON_CHAIN_MAX_LEN = 10
VALUE_NOT_IN_INPUT_ARR = -1
# hash function

# same as array
arr = [[VALUE_NOT_IN_INPUT_ARR  for _ in range (0, COLLISON_CHAIN_MAX_LEN)] for _ in range (min, max + 1)]

def hash_function(val):
return val % (max - min)

for value in input:
index = hash_function(value)
for i, hashed in enumerate (arr[index]):
if (hashed == value):
# duplicate found
break;
else:
if hashed == VALUE_NOT_IN_INPUT_ARR:
arr[index][i] = value;
if (i == COLLISON_CHAIN_MAX_LEN -1):
# TODO code for hash chain size change here.
pass

break;
else:
# colision with other value hashed to this index,
# still dont know nothing -  do nothing (in python pass)
pass

for chain in arr:
for value in chain:
if (value == VALUE_NOT_IN_INPUT_ARR):
break;
print value

if __name__ == '__main__':

remove_duplicates([1,2, 1, 4, 1, 6, 7, 4, 1, 10,])``````

Comment hidden because of low score. Click to expand.
-1
of 1 vote

This is just interview purpose....can try like this....

int[] arr = {1,2,7, 9, 9, 5, 4, 9};
int[] newarr = new int[arr.length];
StringBuffer valueCheck = new StringBuffer();
StringBuffer valueappend = new StringBuffer();
int newarrcount=0;
for(int i=0;i<arr.length;i++){
if(valueappend.indexOf(valueCheck.append(arr[i]).toString())<0){
newarr[newarrcount++]=arr[i];
valueappend.append(arr[i]);
}
valueCheck = new StringBuffer();
}
for(int i=0;i<valueappend.length()-1;i++){
System.out.println(newarr[i]);
}

Comment hidden because of low score. Click to expand.
0
of 0 vote

Sorted array and insert into array1 ,cost time is n*log(n)+n，but usually the method is not vary fast.

We can designer a simple getHashCode method.
If got hashCode conflict, create new an array named buckets store others.

Comment hidden because of low score. Click to expand.
0

``````function remove_duplicates(arr){
var len = arr.length;
var buckets = new Array(len);
var target = new Array(len);
for(var i=0;i<len;i++){
var p = arr[i] % len;
if(!target[p]){
target[p] = arr[i];
}else{
var bucket = buckets[p];
if(bucket){
var exist = false;
for(var n = 0;n<bucket.length;n++){
if(bucket[n] == arr[i]){
exist = true;
break
}
}
if(!exist){
bucket.push(arr[i]);
}
}else{
buckets[p] = [arr[i]];
}
}
}
console.log(target);
console.log(buckets);

var result = [];
for(var i=0;i< target.length ;i++){
if(target[i]){
result.push(i);
}
}
for(var i=0;i<buckets.length;i++){
if(buckets[i]){
for(var j=0;j<buckets[i].length;j++){
result.push(buckets[i][j]);
}
}
}
console.log(result);
}
remove_duplicates([1,2,7, 9, 9, 5, 4, 9]);``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

Can try like this..... :)

int[] arr = {1,2,7, 9, 9, 5, 4, 9};
int[] newarr = new int[arr.length];
StringBuffer valueCheck = new StringBuffer();
StringBuffer valueappend = new StringBuffer();
int newarrcount=0;
for(int i=0;i<arr.length;i++){
if(valueappend.indexOf(valueCheck.append(arr[i]).toString())<0){
newarr[newarrcount++]=arr[i];
valueappend.append(arr[i]);
}
valueCheck = new StringBuffer();
}
for(int i=0;i<valueappend.length()-1;i++){
System.out.println(newarr[i]);
}

Comment hidden because of low score. Click to expand.
-1
of 1 vote

Treeset or Hashmap or hashtable like these.....we can try these for removing duplicates like....

int[] arr = {1,2, 1, 4, 1, 6, 7, 4, 1, 10};
int[] newarr = new int[arr.length];
Map map = new HashMap();
int newarrcount=0;
for(int i=0;i<arr.length;i++){
map.put(new String(arr[i]+""),new Integer(arr[i]));
}

for(Iterator iterator = map.keySet().iterator();iterator.hasNext();){
newarr[newarrcount++]=((Integer)map.get(iterator.next()+"")).intValue();
System.out.println(newarr[newarrcount-1]);
}

Comment hidden because of low score. Click to expand.
0
of 0 vote

what is mean by collection classes here pls explain me....

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````public class ArraysMain {
public static void main(String[] args) {
int[] arr = {1,2,7, 9, 9, 5, 4, 9};
int[] newarr = new int[arr.length];
int k = 0;
for(int i : arr)
{
// check if it there in that arr
if(checkExists(i, newarr))
{
continue;
}
else
{
newarr[k] = i;
k++;
}
}
}

private static boolean checkExists(int i, int[] aa)
{
for(int j : aa)
{
if(j == i)
{
return true;
}
}
return false;
}
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

My java solution using String in O(n):

``````import java.util.Arrays;

public class Remove_Dup_No_Collection {
public static void main(String[] args) {
int[] a = {1,2,7, 9, 9, 5, 4, 9};
System.out.println(Arrays.toString(removeDup(a)));
}

private static int[] removeDup(int[] a) {
String dup = "";

for (int i = 0; i < a.length; i++) {
dup += "" + a[i] + ",";
}
//removing last ","
dup = dup.substring(0, dup.length()-1);
String[] dupArr = dup.split(",");

for (int i = 0; i < dupArr.length; i++) {
dup = dup.replace(dupArr[i], "");
dup += a[i] + ",";
}

String[] ans = dup.split(",");
int count = 0;
for (int i = 0; i < ans.length; i++) {
if(!ans[i].equals(""))
count++;
}
int[] newArr = new int[count];
count = 0;
for (int i = 0; i < ans.length; i++) {
if(!ans[i].equals(""))
newArr[count++] = Integer.parseInt(ans[i]);
}
return newArr;
}
}``````

Output:

``[1, 2, 7, 5, 4, 9]``

Comment hidden because of low score. Click to expand.
0

Comment hidden because of low score. Click to expand.
0

This does not seem to be efficient with regard to space complexity.

Comment hidden because of low score. Click to expand.
0

Integer array is converted to a String, i.e:

``dup = "1,2,7,9,9,5,4,9";``

then it is converted to String array:

``String[] dupArr = dup.split(",");//[1, 2, 7, 9, 9, 5, 4, 9]``

Then all occurrences of a number are removed then the number is added to the string:

``````dup = dup.replace(dupArr[i], "");
dup += a[i] + ",";``````

Finally, the integer array is reconstructed without duplicate.

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````Java already has implemented Merge-sort.
Run-time complexity: On avg, nlogn.
Space complexity: O(n).
static int[] removeDups(final int[] array)
{
int[] copyArray = array.clone();
Arrays.sort(copyArray);

int[] newArray=new int[array.length];

int endMarker=0;

newArray[0]=copyArray[0];
endMarker++;

for(int i=1;i<copyArray.length;i++)
{
if(copyArray[i]!=copyArray[i-1])
{
newArray[endMarker++]=copyArray[i];
}
}

newArray=null;
copyArray=null;
int[] resultArray=new int[endMarker];

for(int i=0;i<endMarker;i++)
{
resultArray[i]=copyArray[i];
}

return resultArray;
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.lang.reflect.Array;

public class Duplicate {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

int a[]={1,3,4,4,5,6,6,66};
int[] b=new int[a.length];
int j=0;
for(int i=0;i<a.length;i++)
{
if(b.length<1 && j<b.length)
{

b[j++]=a[i];
}
else
{
int flag=0;
for(int k=0;k<j;k++)
{

if(a[i]==b[k])
flag=1;
}
if(flag==1)
continue;
else b[j++]=a[i];
}
}
for(int i=0;i<j;i++)
{
System.out.println(b[i]);
}

}

}

Comment hidden because of low score. Click to expand.
0
of 0 vote

just create a binary search tree from the given elements and discard the dublicates

Comment hidden because of low score. Click to expand.
0
of 0 vote

Copy list or array elements to HashSet which removes duplicates, and store back into list or array

Comment hidden because of low score. Click to expand.
0
of 0 vote

Using Array List

``````import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class RemoveSameElements {
{
for(int i = 0 ; i < myArr.size(); i++)
{
if(myArr.get(i) == myArr.get(i+1))
{
myArr.remove(i);
myArr.remove(i);
if(myArr.size() > 1)
{
}
}
}
System.out.println(myArr);
}

public static void main(String[] args) {

int[] arr = {1,2,2,1,4,5,6,6,5};
List<Integer> myArr = new ArrayList<>();
for(int i = 0 ; i < arr.length; i ++)
{
}
}

}``````

Comment hidden because of low score. Click to expand.
-1
of 1 vote

find min and max of the array
then create an array with B[min to max]
for i=0 to i=n
B[a[i]]=1
if(B[a[i]]==1)
this means the element is repeated
in this way it can be done in o(n) time and o(2n ) space

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.