Small nitpick:
if !encountered[elements[v]] == true {
is a double negative and hard to understand. I'd write it as if !encountered[elements[v]] {
or if you must use the bare bool, if encountered[elements[v]] == false {