Skip to content

Two Sum ​

Algorithmic — Given an array and a target sum, find two numbers that add up to it.

Tests: HashMap, iteration, returning a compound value.

Results ​

Language ↕Lines ↑Tokens ↕Verbosity ↕Tok/Line ↕Sym/Line ↕Concepts ↕Safety ↕Ceremony ↕
Python★7▼★31▼★146▼★4.43▼★4.43▼★6▼★3.8▼★0▼
Clojure★8▼★36▼★182▼★4.5▼★5.25▼★5▼★4.3▼★0▼
Ruby★8▼★25▼★111▼★3.13▼★3▼★5▼★3.8▼★0.25▼
Kotlin★9▼★39▼★193▼★4.33▼★4.67▼★7▼★3.9▼★0.222▼
Haskell★10▼★58▼★320▼★5.8▼★4.7▼★11▼★4.8▼★0.2▼
Javascript★10▼★36▼★173▼★3.6▼★4.6▼★6▼★2.3▼★0.3▼
Swift★11▼★46▼★240▼★4.18▼★4.27▼★8▼★4.3▼★0.273▼
Typescript★11▼★45▼★233▼★4.09▼★5.73▼★8▼★3.3▼★0.273▼
C★12▼★60▼★310▼★5▼★5.17▼★7▼★0▼★0.583▼
Erlang★12▼★48▼★257▼★4▼★6.67▼★5▼★4.7▼★0▼
Go★12▼★44▼★216▼★3.67▼★3.25▼★7▼★3.5▼★0.333▼
Rust★12▼★43▼★219▼★3.58▼★5.25▼★13▼★4.8▼★0.333▼
Elixir★13▼★42▼★215▼★3.23▼★4.15▼★6▼★5▼★0.231▼
Zig★13▼★48▼★255▼★3.69▼★5.08▼★13▼★3.3▼★0.385▼
Objc★14▼★59▼★320▼★4.21▼★6.57▼★6▼★0.8▼★0.214▼
Cpp★15▼★54▼★289▼★3.6▼★5.6▼★6▼★1.1▼★0.4▼
Java★15▼★53▼★282▼★3.53▼★4.67▼★10▼★3.1▼★0.467▼
Csharp★16▼★54▼★291▼★3.38▼★3.75▼★8▼★3.7▼★0.438▼
Milo★18▼★85▼★482▼★4.72▼★5▼★15▼★5▼★0.389▼

Observations ​

Simple problem — all languages cluster near 7-15 LOC. Ruby wins on tokens and verbosity: each_with_index + implicit return + no type annotations = minimal syntax.

Haskell is interesting — only 10 lines but highest token count (58) due to pattern matching verbosity and qualified Map.lookup.

Python's enumerate() + dictionary lookup remains the gold standard for clarity.

Solutions ​

def two_sum(nums: list[int], target: int) -> tuple[int, int]:
    seen = {}
    for i, n in enumerate(nums):
        complement = target - n
        if complement in seen:
            return (seen[complement], i)
        seen[n] = i