DEV Community

loading...

Discussion on: Daily Challenge #208 - Delete Occurrences of an Element

Collapse
edh_developer profile image
edh_developer

Java

        static List<Integer> deleteNth(List<Integer> vals, int n) {
                Map<Integer,Integer> numOfInstances = new HashMap<Integer,Integer>();
                List<Integer> updatedList = new ArrayList<Integer>();

                for (Integer i : vals) {
                        Integer currentTotal = numOfInstances.get(i);
                        if (currentTotal == null)
                                currentTotal = 1;
                        else
                                currentTotal += 1;

                        numOfInstances.put(i, currentTotal);

                        if (currentTotal <= n)
                                updatedList.add(i);
                }

                return updatedList;
        }