简体   繁体   中英

Removing N duplicates from integer array

problem statement: I have to remove n duplicates from array.

Here is the full problem statement: https://pastebin.com/EJgKUGe3

and my solution is:

public class minion_labour_shift_2ndTry {
    static int[] data = {1,2, 2, 3, 3, 3, 4, 5, 5};   

    public static void main(String[] args) {

        Scanner reader = new Scanner(System.in);
        int n = reader.nextInt();

        data = answer(data, n);
        for (int i = 0; i < data.length; i++) {
            System.out.print(data[i] + " ");
        }

    }

    public static int[] answer(int[] data, int n) {
        if (data.length>99){
            System.exit(0);
        }
        int[] result = new int[99];
        ArrayList<Integer> temp = new ArrayList<>();
        int counter = 0, count ,maxCount = 0;

        for (int i = 0; i < data.length; i++) {
            boolean isDistinct = false;
            for (int j = 0; j < i; j++) {
                if (data[i] == data[j]) {
                    isDistinct = true;
                    break;
                }
            }
            if (!isDistinct) {
                result[counter++] = data[i];
            }
        }
        for (int i = 0; i < counter; i++) {
            count = 0;
            for (int j = 0; j < data.length; j++) {
                if (result[i] == data[j]) {
                    count++;
                }
            }
            System.out.println("....... count"+count);
            if (maxCount <= count){
                maxCount = count;
            }

            if (count <= n){
                temp.add(result[i]);
            }
        }

        if (maxCount-1 < n){
            return data;
        }

        data = new int[temp.size()];
        for (int i = 0; i <temp.size() ; i++) {
            data[i] = temp.get(i);
        }
        return data;
    }
}

Now, my question is, what I am missing and what should I do to pass all the 10 cases.

Thanks In Advance:)

NB:It will be compiled in java 7, and Map,hashset or third-party libraries, input/output operations, spawning threads or processes and changes to the execution environment are not allowed.

Not going to provide a full solution but suggesting a reworking of the algorithm because it's not clear what you're doing, you never explained your actual thoughts of the algorithm. For example, what are you using isDistinct for?

1) Loop through once and compute the frequency of every number. You can just use an array of length 100 since that's all the data inputs will be. As you loop through, keep track of two things: The total number of entries that occur more than n times, as well as which those numbers are

2) Create a resulting array of the appropriate size (calculated from above) and loop through the list again and fill in the elements that didn't cross the threshold.

I misread the requirements initially, this does what is asked:

  public static int[] answer(int[] data, int n) {
    Map<Integer, Integer> counts = new HashMap<>();
    int elementsNeeded = 0;

    for (int i = 0; i < data.length; i++) {
      Integer currentCount = counts.get(data[i]);

      currentCount = currentCount == null ? 1 : ++currentCount;
      counts.put(data[i], currentCount);

      if (currentCount <= n + 1) {
        elementsNeeded += currentCount > n ? -n : 1;
      }
    }

    int[] resultArray = new int[elementsNeeded];
    int j = 0;

    for (int i = 0; i < data.length; i++) {
      if (counts.get(data[i]) <= n) {
        resultArray[j++] = data[i];
      }
    }

    return resultArray;
  }

...and also your own code, slightly altered:

  public static int[] answer2(int[] data, int n) {
    if (data.length>99){
        System.exit(0);
    }
    ArrayList<Integer> temp = new ArrayList<>();
    int count;

    for (int i = 0; i < data.length; i++) {
        count = 0;
        for (int j = 0; j < data.length; j++) {
            if (data[i] == data[j]) {
                count++;
            }
        }

        if (count <= n){
            temp.add(data[i]);
        }
    }


    data = new int[temp.size()];
    for (int i = 0; i <temp.size() ; i++) {
        data[i] = temp.get(i);
    }
    return data;
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM