Skip to content

Instantly share code, notes, and snippets.

@chase-lambert
Last active May 15, 2024 18:14
Show Gist options
  • Save chase-lambert/88d2f13246c718493d93c4ca814e47ca to your computer and use it in GitHub Desktop.
Save chase-lambert/88d2f13246c718493d93c4ca814e47ca to your computer and use it in GitHub Desktop.
rendezvous with cassidoo challenge: 23.02.26
(ns repeated-groups
(:require [clojure.test :refer [deftest is]))
(defn repeated-groups [nums]
(->> nums
(partition-by identity)
(filter #(> (count %) 1))
(into [])))
;; Using transducers instead which gives
;; a huge speedup for larger vecs (shown
;; below):
(defn repeated-groups-transducer [nums]
(let [xform (comp
(partition-by identity)
(filter #(> (count %) 1)))]
(into [] xform nums)))
;; (def nums (into [] (repeatedly 1000000 #(rand-int 10))))
;; (require '[criterium.core :as cc])
;; (cc/quick-bench (repeated-groups nums)) ;; around 271ms
;; (cc-quick-bench (repeated-groups-transduce nums)) ;; around 61ms
;; (= (repeated-groups nums) (repeated-groups-transduce nums)) ;; true
(deftest repeated-groups-test
(is (= (repeated-groups [1 2 2 4 5]) [[2 2]]))
(is (= (repeated-groups [1 1 0 0 8 4 4 4 3 2 1 9 9]) [[1 1] [0 0] [4 4 4] [9 9]])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment