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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:53:27+00:00 2026-05-27T06:53:27+00:00

This program simply is supposed to eliminate duplicates from an array. However, the second

  • 0

This program simply is supposed to eliminate duplicates from an array. However, the second for loop in the eliminate method was throwing an out of bounds exception. I was looking and couldnt see how that could be, so I figured I would increase the array size by 1 so that I would get it to work with the only downside being an extra 0 tacked onto the end.

To my surprise, when I increased tracker[]’s size from 10 to 11, the program prints out every number from 0 to 9 even if I dont imput most of those numbers. Where do those numbers come from, and why am I having this problem?

import java.util.*;
class nodupes 
{
    public static void main(String[] args) 
    {   

        int[] dataset = new int[10];


        //getting the numbers
        for (int i = 0; i <= 9 ; i++)
        {
            Scanner input = new Scanner(System.in);
            System.out.println("Enter a one digit number");
            dataset[i] = input.nextInt();
        }

        int[] answer = (eliminateduplicates(dataset));
        System.out.println(Arrays.toString(answer));
    }

    public static int[] eliminateduplicates(int[] numbers)
    {

        boolean[] tracker = new boolean[11];
        int arraysize = 1; 
        for(int k = 0; k <= 9; k++)
        {

            if(tracker[numbers[k]] == false)
            {
                arraysize++;
                tracker[numbers[k]] = true;
            }

        }
        int[] singles = new int[arraysize];

        for(int l = 0; l <= arraysize; l++)
        {
            if(tracker[l] == true)
            {
                singles[l] = l;

            }


        }

        return singles;
    }
}    

The exception was occuring at this part

     if(tracker[l] == true)

but only when trackers size was 10. At 11 it just prints [0,1,2,3,4,5,6,7,8,9]

EDIT: The arraysize = 1 was a hold over from debugging, originally it was at 0

EDIT: Fixed it up, but now there is a 0 at the end, even though the array should be getting completely filled.

public static int[] eliminateduplicates(int[] numbers)
{

    boolean[] tracker = new boolean[10];
    int arraysize = 0; 

    for(int k = 0; k < numbers.length; k++)
    {

        if(tracker[numbers[k]] == false)
        {
            arraysize++;
            tracker[numbers[k]] = true;
        }

    }
    int[] singles = new int[arraysize];
    int counter = 0;

    for(int l = 0; l < arraysize; l++)
    {
        if(tracker[l] == true)
        {
            singles[counter] = l;
            counter++;
        }


    }

    return singles;
}
  • 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-27T06:53:28+00:00Added an answer on May 27, 2026 at 6:53 am

    Edit like 20 because I should really be asleep. Realizing I probably just did your homework for you so I removed the code.

    arraySize should start at 0, because you start with no numbers and begin to add to this size as you find duplicates. Assuming there was only 1 number repeated ten times, you would’ve created an array of size 2 to store 1 number. int arraysize = 0;

    Your first for loop should loop through numbers, so it makes sense to use the length of numbers in the loop constraint. for( int i = 0; i < numbers.length; i ++)

    For the second for loop: you need to traverse the entire tracker array, so might as well use the length for that (tracker.length). Fewer magic numbers is always a good thing. You also need another variables to keep track of your place in the singles array. If numbers was an array of 10 9s, then only tracker[9] would be true, but this should be placed in singles[0]. Again, bad job from me of explaining but it’s hard without diagrams.

    Derp derp, I feel like being nice/going to bed, so voila, the code I used (it worked the one time I tried to test it):

    public static int[] eliminateduplicates(int[] numbers)
    {
        boolean[] tracker = new boolean[10];
        int arraysize = 0; 
    
        for(int k = 0; k < numbers.length; k++)
        {
            if(tracker[numbers[k]] == false)
            {
                arraysize++;
                tracker[numbers[k]] = true;
            }
        }
    
        int[] singles = new int[arraysize];
    
        for(int l = 0, count = 0; l < tracker.length; l++)
        {
            if(tracker[l] == true)
            {
                singles[count++] = l;
            }
        }
    
        return singles;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This program is meant to generate a dynamic array, however it gives an access
I have a simple program from a C programming book, and it's supposed to
I'm trying to get this simple program to work on windows, but it crashes:
I was making this very simple lex program (just an introductory program). But on
Have a look at this very simple example WPF program: <Window x:Class=WpfApplication1.Window1 xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
I have a simple script which is used to start another program. This other
This program I use has it's own variables to set when you run it,
This program stores pairs in a map, counting the number of times a word
This program reads emails (really just a .txt file structured like an email) and
This program I'm doing is about a social network, which means there are users

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.