Skip to main content
Back to problems
#1436
Easy Algorithms

Destination city

Array Hash Table String
79.5% acceptance
Feb 25, 2026
2310
107
You are given the array paths, where paths[i] = [cityAi, cityBi] means there exists a direct path going from cityAi to cityBi. Return the destination city, that is, the city without any path outgoing to another city. It is guaranteed that the graph of paths forms a line without any loop, therefore, there will be exactly one destination city.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
use std::collections::HashSet;
impl Solution {
  pub fn dest_city(paths: Vec<Vec<String>>) -> String {
    let outgoing: HashSet<&str> = paths.iter().map(|p| p[0].as_str()).collect();
    paths.iter().map(|p| p[1].as_str()).find(|c| !outgoing.contains(c)).unwrap().to_string()
  }
}