Start learning free

LESSON 120 OF 671 · ARRAYS - CHALLENGES

Move to the Front in Java: remove(index) and add(0, item)

Practice Read the full explanation

You get a to-do list items, an ArrayList 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:

  • items has 1 to 10 words.
  • index is from 0 to the length of items minus 1.
  • Every word is made of lowercase English letters.
  • The same word may appear more than once.
STEP 1 OF 3
    Ctrl + Enter to check

    JAVA · ARRAYS - CHALLENGES

    Move To Front in Java, explained

    Problem overview

    The method moveToFront receives a to-do list items (a list of strings) and a number index. It returns the list with the item at index moved to the front. The other items keep their order. The list has one to ten words, index is always a valid position, and the same word may appear more than once.

    For ["wash", "shop", "cook", "read"] and index = 2 the item at index 2 is "cook". It moves to the front, and "wash" and "shop" shift one place right: ["cook", "wash", "shop", "read"]. With ["tea", "coffee"] and index = 0 nothing changes.

    Approach

    Moving an item is two edits: take it out of its old place, then put it in at index 0. remove(index) does the first and hands back the item it removed, and add(0, item) does the second, shifting everything else right. The order matters: the item must be out of the list before it goes back in, or it would be in the list twice.

    When index is 0, the item is removed from the front and added to the front again, so the list ends up unchanged without a special case.

    Step by step

    1. Remove by position with items.remove(index) and keep the returned word in a String named item. The items after it move one place left.
    2. Insert at the front with items.add(0, item). With an index first, add inserts instead of appending, and shifts the others right.
    3. Return items. The method changes the list it was given and returns that same list.

    The solution in Java

    class Solution {
        public List<String> moveToFront(List<String> items, int index) {
            String item = items.remove(index);
            items.add(0, item);
            return items;
        }
    }
    

    Details worth knowing:

    • List has two remove methods: remove(int index) removes by position and returns the removed item, while remove(Object o) removes the first item equal to o and returns a boolean. Here index is an int, so Java picks the first one, and the result fits in a String.
    • The method changes items itself rather than building a new list. Returning it is what hands the result back.

    Edge cases and common mistakes

    • Removing by value. Writing items.remove(items.get(index)) removes the first matching word, not the one at index. For ["call", "pay", "call", "walk"] and index = 2 it produces call pay call walk, not the expected call call pay walk. Duplicates are exactly why the lesson removes by position.
    • Adding before removing. If you insert first, every old item from the front moves right, so the original is now at index + 1. Calling remove(index) afterwards removes the wrong item: on the first example it gives cook wash cook read, and "shop" is lost.
    • Index 0. This works without special handling; the list comes back unchanged.
    • Forgetting to put the item back. The removed word is gone for good unless you add it again.

    Tracing the first example

    Start with ["wash", "shop", "cook", "read"] and index = 2. The call remove(2) takes out "cook" and leaves ["wash", "shop", "read"]. Then add(0, "cook") inserts it in front, which moves the three others one place to the right and gives ["cook", "wash", "shop", "read"]. The words that were in front of the moved item are the only ones whose indexes change, and they all go up by one.

    Testing

    Inputs and the output of the solution:

    • ["wash", "shop", "cook", "read"], index = 2 gives cook wash shop read.
    • ["tea", "coffee"], index = 0 gives tea coffee.
    • ["call", "pay", "call", "walk"], index = 2 gives call call pay walk.
    Keep going with Java500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free