Daily Challenge #208 - Delete Occurrences of an Element dev.to staff on March 19, 2020 Given a list lst and a number N, create a new list that contains each number in lst at most N times without reordering. For example if N = 2, and... Read full post Submit Preview Dismiss Collapse Expand Amin Amin Amin Follow Education École Supérieure de Génie Informatique Work Teacher, Freelance Developer & Chief Technical Officer @ Doc2wheels Joined Aug 17, 2018 • Mar 19 '20 • Edited on Mar 19 • Edited Dropdown menu Copy link Hide JavaScript function deleteNth([item, ...items], maximum, occurrences = {}) { if (item === undefined) { return []; } if (!occurrences[item]) { occurrences[item] = 1; } else { occurrences[item]++; } if (occurrences[item] > maximum) { return deleteNth(items, maximum, occurrences); } return [item, ...deleteNth(items, maximum, occurrences)]; } Collapse Expand David David David Follow I write pretty stable frontend code Location Berlin Work FE Dev Joined Mar 19, 2020 • Mar 19 '20 Dropdown menu Copy link Hide Just joined the community! I guess reducer is overkill here but still 🤷 let elements = {}; function deleteNth(lst, n) { const reducer = (acc, curVal) => { if (elements[curVal]) { elements[curVal] += 1; if(elements[curVal] <= n) { acc.push(curVal) } } else { elements[curVal] = 1; acc.push(curVal); } return acc } const newlst = lst.reduce(reducer, []) console.log(newlst) } Collapse Expand Avalander Avalander Avalander Follow "Biography is irrelevant" - Seven of nine (probably) if she was asked that question. Joined Mar 12, 2018 • Mar 19 '20 Dropdown menu Copy link Hide Hmm... I wonder what happens if deleteNth is invoked more than once :P Collapse Expand David David David Follow I write pretty stable frontend code Location Berlin Work FE Dev Joined Mar 19, 2020 • Mar 19 '20 Dropdown menu Copy link Hide haha, you re right! Missed that one. Moved the obj initialisation inside the func. Collapse Expand Avalander Avalander Avalander Follow "Biography is irrelevant" - Seven of nine (probably) if she was asked that question. Joined Mar 12, 2018 • Mar 19 '20 Dropdown menu Copy link Hide Scala def deleteNth[T] (xs: Seq[T], n: Int): Seq[T] = { val count = Map[T, Int]() withDefaultValue 0 val acc = List[T]() val (_, result) = xs.foldLeft((count, acc)) { case ((count, prev), x) => if (count(x) >= n) (count, prev) else { val items = count(x) val newCount = count.updated(x, items + 1) (newCount, x :: prev) } } result.reverse } Collapse Expand edh_developer edh_developer edh_developer Follow Joined Oct 18, 2017 • Mar 21 '20 Dropdown menu Copy link Hide 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; } Collapse Expand Valts Liepiņš Valts Liepiņš Valts Liepiņš Follow Location Latvia Work Software engineer Joined Jan 9, 2020 • Mar 19 '20 Dropdown menu Copy link Hide Haskell: import Data.List (foldl') import qualified Data.IntMap.Strict as Map deleteNth :: Int -> [Int] -> [Int] deleteNth n = reverse . fst . foldl' fn ([], Map.empty) where fn (out, set) i = case Map.lookup i set of Nothing -> (i:out, Map.insert i 1 set) Just x | x < n -> (i:out, Map.insert i (x+1) set) | otherwise -> (out, set) Collapse Expand Cameron Desautels Cameron Desautels Cameron Desautels Follow Indefatigable Lisp enthusiast. Probably somewhere writing Clojure. Location Austin, TX Work CEO and Founder at CollBox Joined Feb 27, 2019 • Mar 19 '20 • Edited on Mar 19 • Edited Dropdown menu Copy link Hide Clojure (defn delete-nth [xs n] (-> (reduce (fn [[cnts xs'] x] [(update cnts x (fnil inc 0)) (cond-> xs' (< (cnts x 0) n) (conj x))]) [{} []] xs) (second))) recur-based version, for comparison's sake: (defn delete-nth' [xs n] (loop [cnts {}, xs' [], [x & rst] xs] (if x (recur (update cnts x (fnil inc 0)) (cond-> xs' (< (cnts x 0) n) (conj x)) rst) xs'))) Collapse Expand Matt Ellen-Tsivintzeli Matt Ellen-Tsivintzeli Matt Ellen-Tsivintzeli Follow Ultra-fullstack software developer. Python, JavaScript, C#, C. Location Earth Education I am a master of science Pronouns He/him/his/his Work Software Engineer Joined May 2, 2017 • Mar 20 '20 Dropdown menu Copy link Hide Javascript Map: function onlyN(arr, N) { let countMap = new Map(); return arr.filter(el => { if(countMap.has(el)) { countMap.set(el, countMap.get(el)+1) } else { countMap.set(el, 1); } return countMap.get(el) <= N; }); } Collapse Expand Sabin Pandelovitch Sabin Pandelovitch Sabin Pandelovitch Follow Started low with PHP. Climbed the ladder and stumbled upon jQuery. Reached the clouds on NodeJs and ended in heaven with ReactJs. Location Romania Work Full stack JavaScript developer Joined Oct 19, 2019 • Mar 19 '20 Dropdown menu Copy link Hide JS solution const deleteNth = (arr, c) => arr.reduce((a, v) => a.filter(e => e === v).length < c ? [...a, v] : a, []); Collapse Expand Nghia Nguyen Nghia Nguyen Nghia Nguyen Follow My name is Nghia Nguyen. Senior programmer in C++ Location Vietnam Work Senior Programmer at i3 International Inc Joined Mar 20, 2020 • Mar 20 '20 • Edited on Mar 20 • Edited Dropdown menu Copy link Hide C++ std::vector deleteNth(const std::vector& lst, int n) { std::vector out; std::map count; for(auto i : lst) { if(count[i] < n) { count[i] = count[i] + 1; out.push_pack(i); } } return out; } Collapse Expand Empereol Empereol Empereol Follow Joined Dec 16, 2019 • Mar 19 '20 Dropdown menu Copy link Hide TypeScript function deleteNth(lst: number[], n: number): number[] { return lst.reduce( (arr, cur) => (arr.filter(i => i === cur).length < n ? [...arr, cur] : arr), [] ); } Code of Conduct • Report abuse
JavaScript
Just joined the community! I guess reducer is overkill here but still 🤷
Hmm... I wonder what happens if
deleteNthis invoked more than once :Phaha, you re right! Missed that one. Moved the obj initialisation inside the func.
Scala
Java
Haskell:
Clojure
recur-based version, for comparison's sake:Javascript Map:
JS solution
C++
std::vector deleteNth(const std::vector& lst, int n)
{
std::vector out;
std::map count;
for(auto i : lst)
{
if(count[i] < n)
{
count[i] = count[i] + 1;
out.push_pack(i);
}
}
return out;
}
TypeScript