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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:26:10+00:00 2026-05-19T23:26:10+00:00

I have a bunch of events with their location being pulled from the db

  • 0

I have a bunch of events with their location being pulled from the db and I am doing the following:

List<Place> placeList = new List<Place>();
foreach (var item in eventsList)
{
    bool coordinateExist= placeList.Where(x => x.latitude == item.Latitude && x.longitude == item.Longitude).Count()>0;
    Place place;
    if (coordinateExist)
        place = new Place() { title = item.Title, latitude = item.Latitude+0.0001, longitude = item.Longitude};
    else
        place = new Place() { title = item.Title, latitude = item.Latitude, longitude = item.Longitude};

    placeList.Add(place);
}

I have a slight issue though. If two locations have the exact same latitude and longitude I want to offset the latitude by the slightest amount (equiv to a few meters maybe?), and then add it to placeList. Hence I will have a list full of unique coordinates.

how can i achieve this nicely? The above works but not nice IMO

  • 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-19T23:26:11+00:00Added an answer on May 19, 2026 at 11:26 pm

    The method you choose to do this will depend on how many items you expect to put into the list and how fast you want your list population to be.

    If the list is very small, you can check the list for identical places and then update your latitude, check again, etc. That is:

    foreach (var item in eventsList)
    {
        var latitude = item.Latitude;
        var longitude = item.Longitude;
        while (placeList.Any((p) => p.latitude == latitude && p.longitude == longitude))
        {
            // adjust item latitude
        }
        Place place = new Place() { title=item.Title, latitude = latitude, longitude = longitude };
        placeList.Add(place);
    }
    

    As you can imagine, that gets hugely expensive if the number of events is large, because you have to search the entire list at least once for every item that you add.

    If that becomes too expensive, you can maintain a HashSet of latitude/longitude pairs, and check it as you’re adding items. Come to think of it, you could just build the thing as a HashSet and then convert it to a list:

    class Place: IEquatable<Place>
    {
        public int longitude { get; set; }
        public int latitude { get; set; }
        public string title { get; set; }
    
        public Place (int long, int lat, string ttl)
        {
            // initialization here
        }
    
        public bool Equals(Place other)
        {
            return (this.longitude == other.longitude) && (this.latitude == other.latitude);
        }
    }
    
    ...
    HashSet<Place> Places = new HashSet<Place>();
    
    foreach (var item in eventsList)
    {
        var place = new Place(item.Title, item.Longitude, item.Latitude);
        while (!Places.Add(place))
        {
            // Adjust place.Latitude
        }
    }
    
    // Finally, convert it to a list.
    List<Place> Placelist = Places.ToList();
    

    That still has potential pathological behavior if you have a lot of events with the same latitude/longitude. You can minimize those effects by adding a small random amount when you adjust the latitude. That is, rather than just adding 2 meters or whatever, add a random amount between 1 and 10 meters, for example. Or, you might want to randomly move the longitude, too.

    If you were to do as @anon suggested: build the list with the duplicates and then remove them later, you’d end up doing something very similar. That is, you’d probably build a HashSet of visited locations and test against it while removing the dupes.

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

Sidebar

Related Questions

I have a bunch of events coming in and I have to execute ALL
I have a bunch of files in a folder checked out from a repository.
I have a bunch of controls on a form and all of their change
I have a bunch of menu links and want to change their style on
I have two buttons, and their click events perform an ajax operation. I need
I have a Django website that basically just displays a bunch of items from
I have a bunch of ajax functions I'm firing up on events (click, dblclick,
I have a bunch of objects and I try and simulate events with them.
I have a bunch of events with values (how many people went through a
I have a bunch of regex within PHP, with the first clause being: if

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.