Delete occurrences of an element if it occurs more than n times

Enough is enough!

Alice and Bob were on a holiday. Both of them took many pictures of the places they've been, and now they want to show Charlie their entire collection. However, Charlie doesn't like this sessions, since the motive usually repeats. He isn't fond of seeing the Eiffel tower 40 times. He tells them that he will only sit during the session if they show the same motive at most N times. Luckily, Alice and Bob are able to encode the motive as a number. Can you help them to remove numbers such that their list contains each number only up to N times, without changing the order?
Task

Given a list lst and a number N, create a new list that contains each number of lst at most N times without reordering. For example if N = 2, and the input is [1,2,3,1,2,1,2,3], you take [1,2,3,1,2], drop the next [1,2] since this would lead to 1 and 2 being in the result 3 times, and then take 3, which leads to [1,2,3,1,2,3].
Example

EnoughIsEnough.deleteNth(new int[] {20,37,20,21}, 1) // return [20,37,21]
EnoughIsEnough.deleteNth(new int[] {1,1,3,3,7,2,2,2,2}, 3) // return [1, 1, 3, 3, 7, 2, 2, 2]

Good Solution1:

import java.util.*;

public class EnoughIsEnough {

  public static int[] deleteNth(int[] elements, int max) {
  
    if (max < 1) return new int[0];
    
    final HashMap<Integer,Integer> map = new HashMap<>();
    final List<Integer> list = new ArrayList<>();
    
    for (final Integer i : elements) {
      final Integer v = map.put(i, map.getOrDefault(i, 0) + 1);
      if (v == null || v < max) list.add(i);
    }
    
    return list.stream().mapToInt(i->i).toArray();
  }

}

Good Solution2:

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

class EnoughIsEnough {
    private static boolean shouldAdd(final Map<Integer, Integer> counts, final int element, final int maxOcurrences) {
        if (counts.getOrDefault(element, 0) < maxOcurrences) {
            counts.merge(element, 1, Integer::sum);
            return true;
        }
        return false;
    }

    static int[] deleteNth(final int[] elements, final int maxOcurrences) {
        final Map<Integer, Integer> counts = new HashMap<>();
        return Arrays.stream(elements)
            .filter(element -> shouldAdd(counts, element, maxOcurrences))
            .toArray();
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,005評(píng)論 0 23
  • 寫這個(gè)文的時(shí)候是因?yàn)榭吹酱髮W(xué)一位同學(xué)C坐在同聲傳譯的房間里,帶著耳機(jī),正在翻譯的樣子。估計(jì)你不會(huì)想到,幾年前他和我...
    抹茶花開閱讀 374評(píng)論 10 8
  • 董沛沛 洛陽(yáng) 焦點(diǎn)網(wǎng)絡(luò)五期 堅(jiān)持原創(chuàng)分享第六十二天 兩天出游回來,趕快讀幾頁(yè)書。 焦點(diǎn)解決壓力模式之語...
    緣源流長(zhǎng)閱讀 220評(píng)論 0 0
  • 逍遙在孤獨(dú)的林子 溫麻新姿 熬不過冬天的人,一定看不到春天!挺不住苦痛的人,一定得不到幸福!無論是破繭成蝶,還是鷹...
    溫麻新姿A閱讀 376評(píng)論 2 5
  • 從公司這樣到現(xiàn)在好像還沒休息,請(qǐng)假的時(shí)候也是滿北京城跑的面試,轉(zhuǎn)天又輾轉(zhuǎn)去仲裁,經(jīng)過一天的時(shí)間終于先把那個(gè)文件遞交...
    狗蛋君閱讀 163評(píng)論 0 0

友情鏈接更多精彩內(nèi)容