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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:14:55+00:00 2026-05-30T22:14:55+00:00

I have a general programming question, that I have happened to use Java to

  • 0

I have a general programming question, that I have happened to use Java to answer. This is the question:

Given an array of ints write a program to find out how many numbers that are not unique are in the array. (e.g. in {2,3,2,5,6,1,3} 2 numbers (2 and 3) are not unique). How many operations does your program perform (in O notation)?

This is my solution.

int counter = 0;


for(int i=0;i<theArray.length-1;i++){
for(int j=i+1;j<theArray.length;j++){
    if(theArray[i]==theArray[j]){
        counter++;
                    break; //go to next i since we know it isn't unique we dont need to keep comparing it.
            }
}
}

return counter:

Now, In my code every element is being compared with every other element so there are about n(n-1)/2 operations. Giving O(n^2). Please tell me if you think my code is incorrect/inefficient or my O expression is wrong.

  • 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-30T22:14:56+00:00Added an answer on May 30, 2026 at 10:14 pm

    Why not use a Map as in the following example:

    // NOTE! I assume that elements of theArray are Integers, not primitives like ints
    // You'll nee to cast things to Integers if they are ints to put them in a Map as
    // Maps can't take primitives as keys or values
    Map<Integer, Integer> elementCount = new HashMap<Integer, Integer>();
    for (int i = 0; i < theArray.length; i++) {
       if (elementCount.containsKey(theArray[i]) {
         elementCount.put(theArray[i], new Integer(elementCount.get(theArray[i]) + 1));
       } else {
         elementCount.put(theArray[i], new Integer(1));
       }
    }
    
    List<Integer> moreThanOne = new ArrayList<Integer>();
    for (Integer key : elementCount.keySet()) { // method may be getKeySet(), can't remember
       if (elementCount.get(key) > 1) {
          moreThanOne.add(key);
       }
    }
    
    // do whatever you want with the moreThanOne list
    

    Notice that this method requires iterating through the list twice (I’m sure there’s a way to do it iterating once). It iterates once through theArray, and then implicitly again as it iterates through the key set of elementCount, which if no two elements are the same, will be exactly as large. However, iterating through the same list twice serially is still O(n) instead of O(n^2), and thus has much better asymptotic running time.

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

Sidebar

Related Questions

This question concerns the reflection mechanisms of the java programming language. I have an
This is a general programming question (it doesn't have a specific purpose/application yet). If
This is a general question on programming style. Let's say I have an object
This is a general programming question. I'm learning about C++ and I've learned that
I have a general question that is rather open-ended (i.e. depends on platform, application
I have a general question about the way that database indexing works, particularly in
Note: This is a general programming question, not specific to any language, but feel
I have a general question about this. When you have a gallery, sometimes people
Although this is django related, it's really just a general, programming efficiency question. I
I'm kinda new to WPF and .Net programming in general. I have downloaded this

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.