Australia’s Ariarne Titmus beat the defending American champion Katie Ledecky to win 400m freestyle gold.