In the clojure.set
namespace we can find the function map-invert
. This function returns a new map where the values are keys with the appropriates keys of the original map assigned as value. If the original map has duplicate values than the latest key for the duplicate value will be the value of the new key.
In the following example code we see the result of using map-invert
:
(ns mrhaki.set.map-invert (:require [clojure.set :refer [map-invert]] [clojure.test :refer [is]])) (is (= {"mrhaki" :alias "Clojure" :language} (map-invert {:alias "mrhaki" :language "Clojure"}))) ;; With duplicate values only one will be key. (is (= {1 :c 2 :b 3 :d} (map-invert {:a 1 :b 2 :c 1 :d 3})))
Written with Clojure 1.10.1.