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 6326329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:04:09+00:00 2026-05-24T17:04:09+00:00

Im having difficulty understanding how the array is being copied in this algorithm. Im

  • 0

Im having difficulty understanding how the array is being copied in this algorithm. Im am fine with the code till i get to line 12 shown below. This code was from a book Im currently studying.
Im just trying to understand how the copy is carried out on each match

Wrote some values to simulate the a scenario

a = [10,4,6,10,5,2]

a[0] = a[3] MATCH occurs when j=3 

arraycopy(a, 4, a, 3, (5-3)) // Variable values substituted into arraycopy. (pretty sure they're correct)            

System.arraycopy(a, j+1, a, 0, n-j); // Line 12   

The whole code:

    int n = a.length;

    if (n < 2){
        System.out.println("no duplicates");

    }

    for (int i = 0; i< n-1; i++)
        for (int j = i + 1; j < n; j++ )
            if(a[i] == a[j]){
                --n;                    
                System.arraycopy(a, j+1, a, 0, n-j); // Line 12   
                --j;                    


                System.out.println();
            }
    int[] aa = new int[n];
    System.arraycopy(a, 0, aa, 0, n);

Just for the record I understand the second arraycopy statement since its just a straightforward copy into array aa.

I know there are many arraycopy questions on SO but the ones i encountered did not answer this specific question because i need an step-through understanding of how the copying occurs match by match.

Anyway thanks in advance guys.

  • 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-05-24T17:04:10+00:00Added an answer on May 24, 2026 at 5:04 pm

    I’m pretty sure there’s a bug in the algorithm, since the first array copy will overwrite good data at the front of the array. I think the line should be:

    System.arraycopy(a, j+1, a, j, n-j); // Line 12
    

    This would shift the end of the array over one, overwriting only the one duplicate that was just found.

    Right now, it works like this:

    Before copy:
     i           j     n
    [1, 2, 3, 4, 1, 5, 6]
                    \  /
                    n-j
    
    After copy:
     i        j     n
    [5, 6, 3, 4, 1, 5, 6]
    

    Clearly wrong. There are now more duplicates than when we started!

    With the updated line, it looks like this:

    After copy:
     i        j     n
    [1, 2, 3, 4, 5, 6, 6]
    

    Much better. The duplicate 1 is gone.

    As an additional note, this is a pretty horrible way to eliminate duplicates from an array. An array that was all duplicates would perform (n * (n – 1) / 2) element copies, which is ludicrous. The standard simple way to dedupe an array is usually to add each element to a HashSet, then iterate that set’s contents into a new array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was given this starter code for a project, but am having difficulty understanding
I've been having some difficulty in understanding the source of a problem. Below is
I'm having some difficulty understanding why the output of the following code is not
I am having difficulty understanding how exceptions are handled when code is fetched dynamically
I've inherited the following Perl code, and I'm having difficulty understanding what exactly the
Question: For typed in commands invoked via M-x I am having difficulty understanding how
I am new to programming and am having difficulty with understanding object lifetime /
I'm working on an ASP.NET application and I'm having difficulty in understanding why a
I'm having a bit of difficulty understanding how the python __init__ ( ) function
Having difficulty articulating this correlated subquery. I have two tables fictitious tables, foo and

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.