Skip to content
AITroveRead. Build. Understand.
Make this comfortable

Java array and text exercises: stable filtering and boundary tests

Last updated: 29 Sept 20264 min read
tutorial
IntermediateBy AITrove Editorial

Stable filtering retains accepted elements in their original order; the contract must define whether the input is mutated and what equality means.

Java 8+. The program uses only JDK classes and runs without a framework.

Separate two small tasks

Task one removes adjacent duplicate shipment codes without changing the input array. Task two checks whether a text field is a palindrome under an ASCII-letter, case-insensitive policy. They use different representations and should not share a generic helper merely because both contain loops.

For the array task, preserve the first code in each run, not the first code seen anywhere in the array. The input [7,7,3,7] must produce [7,3,7]. A HashSet would remove the later 7 and violate the adjacent-only requirement. The implementation uses a separate output array, then trims it to the retained size.

For the text task, spell out normalization. This version drops every non-ASCII letter and compares lowercase letters from both ends. That policy is appropriate only for the exercise. It is not a Unicode name validator or a general rule for comparing user text. Unicode units need a wider contract.

Turn examples into checks that can fail

A printed output is useful for reading, but a test should stop when an expected result differs. The program uses explicit checks rather than Java assert statements, which are disabled unless assertions are enabled. It covers separated duplicates, empty input, normalized matching text and a non-match.

Extend the fixture set before changing the implementation. Add an all-equal array, an array with no duplicates, one retained element, punctuation-only text and mixed case. Null input is rejected by ordinary dereferencing here; add a named validation exception if the public API needs clearer diagnostics.

Working program

Java
import java.util.Arrays;
public class IntakeBoundaryChecks {
    static int[] collapseRuns(int[] shipmentCodes) {
        int[] retained = new int[shipmentCodes.length];
        int count = 0;
        for (int code : shipmentCodes) {
            if (count == 0 || retained[count - 1] != code) retained[count++] = code;
        }
        return Arrays.copyOf(retained, count);
    }
    static boolean asciiPalindrome(String text) {
        String normalized = text.replaceAll("[^A-Za-z]", "").toLowerCase(java.util.Locale.ROOT);
        int left = 0, right = normalized.length() - 1;
        while (left < right) if (normalized.charAt(left++) != normalized.charAt(right--)) return false;
        return true;
    }
    static void require(boolean condition) {
        if (!condition) throw new IllegalStateException("Exercise check failed");
    }
    public static void main(String[] args) {
        require(Arrays.equals(collapseRuns(new int[]{7, 7, 3, 7}), new int[]{7, 3, 7}));
        require(collapseRuns(new int[0]).length == 0);
        require(asciiPalindrome("Never odd or even"));
        require(!asciiPalindrome("parcel"));
        System.out.println("4 boundary checks passed");
    }
}

Output

Output
4 boundary checks passed

Cost and failure boundaries

The array scan and final trim copy take O(n) time and O(n) additional storage. An in-place compaction can use O(1) working storage but needs to return the logical retained length, since an array cannot shrink. That API changes ownership and mutation, so it should be a separate exercise rather than an unnoticed optimization.

The text normalization and two-ended comparison take O(m) work for m text units under this fixed simple pattern, and allocate O(m) normalized text. A direct scan could skip rejected characters without building the normalized string. Keep the same ASCII/case policy when comparing the alternatives.

Common Mistakes

  • Do not substitute global deduplication for adjacent-run removal.
  • Do not use disabled assert statements as the only executable checks.
  • Do not claim an ASCII normalization policy handles every written language.
java
array-string-exercises
Storage details