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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:21:36+00:00 2026-06-11T14:21:36+00:00

Given a sorted list of something (a,a,b,c,c) What would be the most efficient way

  • 0

Given a sorted list of something (a,a,b,c,c)

What would be the most efficient way to recognize that a exists in the list 2 times, b once and c 2 times?

Aside from obvious making a map of counts. Can we do better then this?

            if (map.containsKey(key)) {
                map.put(key, map.get(key) + 1);
            } else {
                map.put(key, 1);
            }

Ultimately the goal is to iterate of the list and know at any given point how many times a key was seen before. Putting things in a map, seems like a step we don’t really need.

  • 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-11T14:21:37+00:00Added an answer on June 11, 2026 at 2:21 pm

    Your method, at each iteration, makes

    • one lookup for containsKey
    • one lookup for get
    • one unboxing from Integer to int
    • one boxing from int to Integer
    • one put

    You could simply compare the current element to the previous one, increment a count if it’s equal, and put the count if not (and reset the counter to 1).

    But even if you keep your algorithm, using get and compare the result to null would at least avoid an unnecessary lookup.

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

Sidebar

Related Questions

Given an NSMutableArray of dynamic CGPoint s, what is the fastest and most efficient
the question is like this: there is a sorted list of n numbers. Given
I'm using Tcl. I have a sorted list of real numbers. Given the number
How would I print the multiples of a list of given numbers in a
I want to remove duplicates from sorted linked list {0 1 2 2 3
Given a sorted list of strings and the last entry is null. You cannot
So if I was given a sorted list/array i.e. [1,6,8,15,40], the size of the
Given a generic List I would need some kind of index (in the database
Given: case class Person(name: String) and trying to do: scala> List(Person(Tom), Person(Bob)).sorted results in
I'm doing some research on ranking algorithms, and would like to, given a sorted

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.