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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:35:54+00:00 2026-05-25T15:35:54+00:00

I had an interview today which involved this very question and in order to

  • 0

I had an interview today which involved this very question and in order to widen my algorithmic knowledge. I am trying to see if there are any better suggestions.

I was trying to find duplicates in an array without using java.util and widen my algorithmical knowledge in regards to addressing space and time complexities.

Below is the code I produced during the technical assessment:

 public static boolean isThereDuplicates(int[] A){
    
    for (int i = 0; i < A.length; i++)
        for (int j = i + 1; j < A.length; j++){
            if (A[i] == A[j])
                return true;
        }
   
            return false;
}

This simple algorithm looks identical to the Bubble Sort, which runs in O(N^2). Is there any other better algorithms that I could use to achieve this?

  • 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-25T15:35:55+00:00Added an answer on May 25, 2026 at 3:35 pm

    If the values of A are reasonably bounded (i.e. you have enough RAM) you could use the bones of the radix-sort algorithm to find a duplicate in O(n).

    public static boolean containsDuplicates(int[] A)
    {
        // Create a zero-initialised array the size of the maximum allowed value in A.
        int[] count = new int[maximumValuePossible(A)];
    
        for (int i = 0; i < A.length; i++)
        {
            if (count[A[i]] != 0)
            {
                // The value at A[i] is already in the histogram -> duplicate!
                return true;
            }
    
            // A[i] is not in the histogram yet.
            count[A[i]]++;
        }
    
        return false;
    }
    

    Edit: To return a copy of the array with duplicates removed you could then do:

    public static int[] stripped(int[] A)
    {
        int[] count = new int[maximumValuePossible(A)];
        int uniques = 0;
    
        for (int i = 0; i < A.length; i++)
        {
            count[A[i]]++;
            if (count[A[i]] == 1)
            {
                uniques++;
            }
        }
    
        if (uniques == 0) return null;
    
        int[] retArray = new int[uniques];
        int retIndex = 0;
        for (int i = 0; i < count.length; i++)
        {
            if (count[i] > 0)
            {
                retArray[retIndex++] = count[i];
            }
        }
    
        return retArray;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Had this question in the interview yesterday. Which is better to use? Infix(with parenthesis)
I had an interview today and the person asked me this question: How do
I had an interview today and was asked this question! code the MS Paint
In an interview they had asked an Question like this there are three overloading
This is one of an interview question which I had recently. I would like
I heard this today during interview for java developer. I had to list some
I had an interview days ago and was thrown a question like this. Q:
I just got this question on an interview and had no idea how to
Yesterday i had a question in an interview which i thought i could find
Today I had an interview on which I asked candidate quite usual and basic

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.