tail recursion - Return a sequence with the elements not in common to two original sequences by using clojure -
i have 2 sequences, can vector or list. want return sequence elements not in common 2 sequences.
here example:
(removedupl [1 2 3 4] [2 4 5 6]) = [1 3 5 6] (removeddpl [] [1 2 3 4]) = [1 2 3 4]
i pretty puzzled now. code:
(defn remove-dupl [seq1 seq2] (loop [a seq1 b seq2] (if (not= (first a) (first b)) (recur (rest b)))))
but don't know next.
i encourage think problem in terms of set operations
(defn extrasection [& ss] (clojure.set/difference (apply clojure.set/union ss) (apply clojure.set/intersection ss)))
such formulation assumes inputs sets.
(extrasection #{1 2 3 4} #{2 4 5 6}) => #{1 6 3 5}
which achieved calling (set ...) function on lists, sequences, or vectors.
even if prefer stick sequence oriented solution, keep in mind searching both sequences o(n*n) task if scan both sequences [unless sorted]. sets can constructed in 1 pass, , lookup fast. checking duplicates o(nlogn) task using set.
Comments
Post a Comment