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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:09:42+00:00 2026-06-16T19:09:42+00:00

I have a problem statement which is working but still i would like to

  • 0

I have a problem statement which is working but still i would like to know more efficient, faster and more importantly correctly designed to handle the below mentioned scenario.

I have a POJO class

class A {
  String s;
  Double d;
}

I am trying to populate a List, basically a List of Object A into the list. Now the implementation in question. While adding the Object A into the list i need to check if an Object with String s already exists. If yes i want to update the older Object with old d1 + new d1 and do not add the new Object to the list, If no add the new Object to the list. My present implementation is something like below.

double dd = 0.0;
    List<A> aList = new List<A>();
    List<A> aListToRemove = new List<A>();
    A newA = null;
    for(int i=0;i<=100;i++ ){
        newA = method call which returns newA;
        for(A oldA: aList ){
            if(oldA.getS().equals(newA.getS())){
                dd = oldA.getD() + newA.getD();
                newA.setD(dd);
                aListToRemove.add(oldA);
            }
            aList.add(newA);
            aList.removeAll(aListToRemove);
        }
    }

//at the end, i would like to see aList with no duplicates, but with updated d value.

Is there a more efficient way to do the processing within the second for loop?

  • 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-16T19:09:43+00:00Added an answer on June 16, 2026 at 7:09 pm

    It seems you could use a map for your use case:

    Map<String, A> map = new HashMap<> ();
    

    and put items in the map like this:

    map.put(someA.s, someA);
    

    That should turn your O(n^2) algoritm into an O(n) algorithm.

    When you receive a newA, you can use the following:

    A a = map.get(newA.getS());
    if (a == null) {
        map.put(newA.getS(), newA); //new string => new item in the map
    } else {
        a.setD(a.getD() + newA.getD()); //found string => updating the existing item
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem which i thought one of the questions would solve at
I have one particular problem, I have script which should issue bill statement. Now
I have this banner rotator which is working fine except for one problem... This
I'm using Log4Net 2.0, I have it working as required but which to add
I have a problem which i have simplified below. I have a sql statement
I have some code thats not working(which is a common occurrence for me), but
Problem statement We have one employer that wants to interview N people, and therefore
I have a problem with a continue statement in my C# Foreach loop. I
I have a problem with the SQL statement detailed below. The query returns the
I have a problem creating the following SQL Statement using LINQ & C# select

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.