0

I am trying to get pass a coding challenge. The goal is to remove duplicate (after a defined 'n-th' time) from the array.

For example,

int[] arr;
arr = new int[] {1, 2, 2, 3, 3, 3, 4, 5, 5};

arr = tester(arr,1);//return 1,4. anything having more then 1 occurrence is removed from the //array

I have 2 questions here.

  1. I understand that although java is mainly call by value, more detail: https://stackoverflow.com/questions/12757841/are-arrays-passed-by-value-or-passed-by-reference-in-java#:~:text=Longer%20answer%3A,object%20that%20the%20caller%20sees. and Is Java "pass-by-reference" or "pass-by-value"? . so am I not able to modify/return the value of arr without re-assigning it as I need to use the "new" keyword later on. example: I am not able to do the following:

    tester(arr,1) //return the original value, as the method has a "new" when converting the //arraylist into array. There seems to be no work around for this as well..

  2. I am also only passing 2 out of 10 test case in the coding challenge, I am not very sure why. I have also attempted to error handle with string inputs, or length=0 or null, to no success. (or implemented it in hashmap for sake of time complexity) It does not seem like my logic has an issue, I am just not sure what are the test case as it is hidden.

I believe part of the challenge requires me to return it in the original array, meaning changing the value of arr itself, but i cant find a way to do it without using the new keyword. Any ideas anyone?

public static int[] tester(int[] data, int n)
    {
ArrayList<Integer> storeNon_dup = new ArrayList<Integer>();
    
   //nested for loop to run through the array
   //store in arrayList if criteria valid
    for(int i = 0; i < data.length; i++)
    { 
        int counter = 0;
        for(int j = 0; j< data.length; j++)
        {
            if(data[i] == data[j])
            {
                counter++;
            }
            
        }
        //if not duplicate in n-th occurence, add to list
        if(counter<=n)
        {
            storeNon_dup.add(data[i]);
        }
    }
    
    //convert arraylist to array
    int[] container = new int[storeNon_dup.size()];
    for(int i = 0; i<storeNon_dup.size(); i++)
    {   
        container[i] = storeNon_dup.get(i);
    }
  
    return  container;
}
Dilip Meghwal
  • 632
  • 6
  • 15

1 Answers1

0

Alternate solution by using HashMap.

public static List tester(int[] data, int n) {
    HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
    for(int i=0; i<data.length; i++) {
        if(map.containsKey(data[i])) {
            map.put(data[i], map.get(data[i])+1);
        }else {
            map.put(data[i], 1);
        }
    }
    List lst = new ArrayList<Integer>();
    for(Integer key : map.keySet()) {
        if(map.get(key)<=n) {
            lst.add(key);
        }
    }
    return lst;
}
Dilip Meghwal
  • 632
  • 6
  • 15
  • Hey Dilip, many thanks. I tried a hashmap as well, but it did not result in any more success in the test case. Any other ideas? – invertedOwlCoding Sep 22 '20 at 04:11
  • Ohh I got it wrong you have to remove the element only if it reach the N occurrence else this should be in the list. Updated the code `if(map.get(key)<=n)` condition, try now. – Dilip Meghwal Sep 22 '20 at 04:16
  • Apologies for the confusion, i meant i have already attempted with hashmap (and it produced the expected result) however, it did not manage to get pass any more test case. – invertedOwlCoding Sep 22 '20 at 04:18