Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8809895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:59:23+00:00 2026-06-14T02:59:23+00:00

In Java, we have an array int[] a = new int[10000000]; fully filled in

  • 0

In Java, we have an array int[] a = new int[10000000]; fully filled in with arbitrary numbers. Quite often in code we need to remove an arbitrary subsequence: that is a set of elements that may be non-contiguous.

The reason to use int[] over LinkedList is a speed gain while passing through elements. Currently there is no removal of elements, so lots of rubbish is stored for the time of running application. Removing elements may give a speed-up, so quite an interesting question.

How to remove a subseqence from an array in a fastest possible way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T02:59:24+00:00Added an answer on June 14, 2026 at 2:59 am

    It depends on whether you want to shorten the array or if you can allow unused elements at the end of the array. The tool for this is System.arraycopy. To shorten the array, you will need to allocate a new one:

    public int[] remove(int[] original, removeStart, removeEnd) {
        int originalLen = original.length;
        int[] a = new int[originalLen - removeEnd - removeStart];
        System.arraycopy(original, 0, // from original[0]
            a, 0,                     // to a[0]
            removeStart);             // this many elements
        System.arraycopy(original, removeEnd, // from original[removeEnd]
            a, removeStart,                   // to a[removeStart]
            originalLen - removeEnd);         // this many elements
        return a;
    }
    

    To just compact an array:

    System.arraycopy(array, removeEnd, // from array[removeEnd]
        array, removeStart,            // to array[removeStart]
        array.length - removeEnd);     // this number of elements
    

    You don’t have to worry about overlapping ranges; arraycopy correctly deals with those.

    If you have a discontinuous range of elements to remove, you can either generalize one of these solutions (less moving things around, but more complex code) or you can remove each continuous block separately (easier to program but you will be moving around data that you will be discarding).

    If you have scattered indices to remove, I would do it by hand. The design depends on whether it is scattered individual indices or whether it is a collection of ranges. With the latter (this is untested, but it should give you the idea):

    /**
     * Simple class to hold the start and end of a range.
     */
    public static class Range implements Comparable<Range> {
        int start;
        int end;
        public int compareTo(Range other) {
            if (start < other.start) return -1;
            if (start > other.start) return 1;
            if (end < other.end) return -1;
            if (end > other.end) return 1;
            return 0;
        }
    }
    /**
     * Remove a list of ranges from an array.
     * @param original the array from which to remove the values.
     * @param toRemove the list of ranges to remove. This must be
     *    sorted in ascending order of range start before calling this method.
     * @param compact flag indicating whether to simply compact the original
     *    array or to copy the values into a new array. If false, will allocate
     *    a new array of the exact size needed to contain the elements not removed.
     */
    public int[] remove(int[] original, List<Range> toRemove, boolean compact) {
        int[] a;
        if (compact) {
            a = original;
        } else {
            int len = 0;
            for (Range range : toRemove) len += range.end - range.start;
            a = new int[original.length - len];
        }
        int nextSource = 0;
        int nextDest = 0;
        for (Range range : toRemove) {
            if (nextSource < range.start) {
                System.arraycopy(original, nextSource, a, nextDest,
                    range.start - nextSource);
                nextDest += range.start - nextSource;
                nextSource = range.start;
            }
            nextSource = range.end;
        }
        if (nextSource < original.length) {
            System.arraycopy(original, nextSource, a, nextDest,
                original.length - nextSource);
        }
        return a;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array, for example (in Java) int[] a = new int[N]; I
Suppose that I have a 2D array (matrix) in Java like this... int[][] MyMat
I have a Java array defined already e.g. float[] values = new float[3]; I
I have a byte array, received from java application via network. I need to
Let's say I have a 2D array: int[][] a = new int[4][3]; populated such
I have an array of Integers in Java, I would like use only a
I have an array of unsigned integers in C and a java array of
I'm a Java programer lost in C++ and pointers :D I have an array
I have a 10x10 array in Java, some of the items in array which
I have a small program to do in Java. I have a 2D array

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.