Skip to the editor
JSGroundwork
JSGroundwork handwritten · web dev
←Back to Two pointers

Ready to read

JSJavaScript⑂Git◎Interview prepΣDSA in JSSDSystem Design
More topics15›
</>HTML{ }CSS⚛ReactNNext.jsNeNest.jsTSTypeScriptNoNode.js🐳DockerDBSQL & Databases✓Testing🔒Web Security☁Cloud & DevOps◈GraphQL◆Redis☸Kubernetes
☰‹›
Description
All topics›JavaScript›Two pointers

Merge Two Sorted Arrays

beginnerlayer B4 · Two pointers5 tests

Given two arrays a and b, each already sorted in non-decreasing order, return a new array containing all their elements in non-decreasing order.

  • Do not mutate a or b
  • Duplicates are kept — if a value appears in both inputs it appears twice in the output
  • Either array may be empty
  • Concatenating and calling .sort() is O(n log n) and misses the point. Aim for O(n + m) with two pointers

Stuck?

All exercisesPlayground
1234567891011121314
loading the editor…

Nothing yet — hit Run and whatever you log shows up here.

Submit to see how you did.

⌘/Ctrl + Enter run⌘/Ctrl + S save⌘/Ctrl + / comment⌘/Ctrl + F find/replace⌘/Ctrl + D select next matchAlt + click multi-cursorTab indent · ⇧Tab outdentEsc leave fullscreen