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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:32:48+00:00 2026-06-15T01:32:48+00:00

I have to find the element with highest occurrences in a double array. I

  • 0

I have to find the element with highest occurrences in a double array.
I did it like this:

int max = 0;
for (int i = 0; i < array.length; i++) {
       int count = 0;
       for (int j = 0; j < array.length; j++) {
         if (array[i]==array[j])
             count++;
   }
  if (count >= max)
      max = count;
 }

The program works, but it is too slow! I have to find a better solution, can anyone help me?

  • 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-06-15T01:32:50+00:00Added an answer on June 15, 2026 at 1:32 am

    Update:

    • As Maxim pointed out, using HashMap would be a more appropriate choice than Hashtable here.
    • The assumption here is that you are not concerned with concurrency. If synchronized access is needed, use ConcurrentHashMap instead.

    You can use a HashMap to count the occurrences of each unique element in your double array, and that would:

    • Run in linear O(n) time, and
    • Require O(n) space

    Psuedo code would be something like this:

    • Iterate through all of the elements of your array once: O(n)
      • For each element visited, check to see if its key already exists in the HashMap: O(1), amortized
      • If it does not (first time seeing this element), then add it to your HashMap as [key: this element, value: 1]. O(1)
      • If it does exist, then increment the value corresponding to the key by 1. O(1), amortized
    • Having finished building your HashMap, iterate through the map and find the key with the highest associated value – and that’s the element with the highest occurrence. O(n)

    A partial code solution to give you an idea how to use HashMap:

    import java.util.HashMap;
    ...
    
        HashMap hm = new HashMap();
        for (int i = 0; i < array.length; i++) {
            Double key = new Double(array[i]);
            if ( hm.containsKey(key) ) {
                value = hm.get(key);
                hm.put(key, value + 1);
            } else {
                hm.put(key, 1);
            }
        }
    

    I’ll leave as an exercise for how to iterate through the HashMap afterwards to find the key with the highest value; but if you get stuck, just add another comment and I’ll get you more hints =)

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

Sidebar

Related Questions

I have a table. In this table have select element. How can I find
In this code, If I have to find an element '7', it points to
I have an HTML document and I would like to find the HTML element
I have to find an element by its id with jQuery. This element has
I have a Find function in order to find an element from a BST
I have a std::vector with the element as pair and need to find all
How do I find the width of an element? I have maxwidth set to
I need to find an a -tag's ID by its style. My element have
I have an array of integers and I'm trying to find which one is
I have array of 1's and 0's only. Now I want to find smallest

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.