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

  • Home
  • SEARCH
  • 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 978369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:02:54+00:00 2026-05-16T04:02:54+00:00

Below is the problem description and algorithm that I have written. Is there anything

  • 0

Below is the problem description and algorithm that I have written. Is there anything to be done to improve this algorithm?

Given an integer array of unknown size, containing only numbers between 0 and 30, write a function to return an integer array containing all of the duplicates.

int[] findDupes(int[] array) {
    int[] found = new int[30];
    int[] dupes = new int[30];
    int dupesCount = 0;
    for (int i = 0; i < array.length; i++) {
        if (found[array[i]] <= 1) {
            found[array[i]]++;              
        }else{
            continue;
        }
        if(found[array[i]] > 1){
            dupes[dupesCount++] = array[i];
            if (dupesCount == 30)
                break;
        }
    }
    if (dupesCount == 0)
        return new int[0];
    return dupes;
}

Am assuming that the best case for running this algorithm would n or 30 whichever is lower
and the worst case for running this algorithm is n, since I have to scan the entire array to find duplicates. Any comments?

  • 1 1 Answer
  • 3 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-16T04:02:54+00:00Added an answer on May 16, 2026 at 4:02 am

    here is the modified version with comments embedded.

    int[] found = new int[3];
        int[] dupes = new int[3];
        int dupesCount = 0;
        for (int i = 0; i < array.length; i++) {
            if (found[array[i]] <= 1) {
                found[array[i]]++;              
            }
            if(found[array[i]] > 1){ //duplicate found
                dupes[dupesCount++] = array[i];
    
                // if 30 duplicate are found don't scan the array any more
                // since the distinct numbers are only 30
                if (dupesCount == 30) 
                    break;
            }
        }
        if (dupesCount == 0)
            return null;
        return dupes;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with the query below in postgres SELECT u.username,l.description,l.ip,SUBSTRING(l.createdate,0,11) as createdate,l.action
I know that there must be a really simple solution to the below problem
I wonder if there any better ideas to solve the problem below, I have
I have one problem Looking below: I have this model: class Shoes(models.Model): shop =
UPDATE: I've realized that the below problem only occurs if the user has already
i have a problem with below's code, onProgressUpdate is not running ... however the
I'm in search of an algorithm, which can handle the problem described below. I
OP, please replace this text with a detailed description of your problem. Your code
I have a situation like below in which I have some problem in freeing
I have file writing permission denied problem in my web application, below is the

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.