Skip to the code
cf.
+25 XP

#109 Keep Two of Each Retry

medium

A request logger records the error code of every failed request in codes, sorted ascending. To keep the log short, it keeps at most two entries for each code and drops the rest.

Write keepTwo(codes) that does this in place: each code appears at most twice, in ascending order, and the array is shortened to the kept items. Don't create a second array. The tests check codes after your function runs.

Example 1

Input: codes = [404,404,404,500,503,503,503,503]
Output: [404,404,500,503,503]

404 and 503 are cut down to two entries each.

Example 2

Input: codes = [400,401,401]
Output: [400,401,401]

No code appears more than twice.

Constraints

  • 0 <= codes.length <= 100000
  • codes is sorted ascending.
  • 0 <= codes[i] <= 999
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).