Skip to content

Instantly share code, notes, and snippets.

@chase-lambert
Last active May 17, 2023 00:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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))
(mapv vec)))
;; Using transducers instead which gives
;; a huge speedup for larger vecs (shown
;; below in unscientific fashion):
(defn repeated-groups-transducer [nums]
(let [xform (comp
(partition-by identity)
(filter #(> (count %) 1)))]
(into [] xform nums)))
;; (def nums (into [] (repeatedly 10000000 #(rand-int 10))))
;; (time (repeated-groups nums)) ;; around 4.5 - 5 seconds
;; (time (repeated-groups-transduce nums)) ;; around 1.1 - 1.4 seconds
;; (= (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