You get a to-do list items, a List of strings, and a number index. Return the list with the item at index moved to the front.
Take the item out of its place and put it at index 0. The other items keep their order.
Example 1:
Input: items = ["wash", "shop", "cook", "read"], index = 2 Output: ["cook", "wash", "shop", "read"] Explanation: The item at index 2 is "cook". It moves to the front. Then "wash" and "shop" move one place to the right.
Example 2:
Input: items = ["tea", "coffee"], index = 0 Output: ["tea", "coffee"] Explanation: The item at index 0 is already at the front. Nothing changes.
Example 3:
Input: items = ["call", "pay", "call", "walk"], index = 2 Output: ["call", "call", "pay", "walk"] Explanation: The "call" at index 2 moves to the front. The other "call" and "pay" move one place to the right.
Constraints:
itemshas 1 to 10 words.indexis from 0 to the length ofitemsminus 1.- Every word is made of lowercase English letters.
- The same word may appear more than once.
Free plan: 30 tutor messages. PRO is unlimited.
Saved
Ctrl + Enter to check
