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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:32:36+00:00 2026-05-10T18:32:36+00:00

string[] filesOfType1 = GetFileList1(); string[] filesOfType2 = GetFileList2(); var cookieMap = new Dictionary<string, CookieContainer>();

  • 0
string[] filesOfType1 = GetFileList1(); string[] filesOfType2 = GetFileList2(); var cookieMap = new Dictionary<string, CookieContainer>();  Action<string, Func<string, KeyValuePair<string, CookieContainer>>> addToMap = (filename, pairGetter) =>     {         KeyValuePair<string, CookieContainer> cookiePair;         try         {             cookiePair = pairGetter(filename);         }         catch         {             Console.WriteLine('An error was encountered while trying to read ' + file + '.');             return;         }          if (cookieMap.ContainsKey(cookiePair.Key))         {             if (cookiePair.Value.Count > cookieMap[cookiePair.Key].Count)             {                 cookieMap[cookiePair.Key] = cookiePair.Value;             }         }         else         {             cookieMap.Add(cookiePair.Key, cookiePair.Value);         }     };   foreach (string file in filesOfType1) {     addToMap(file, GetType1FileCookiePair); } foreach (string file in filesOfType2) {     addToMap(file, GetType2FileCookiePair); } 

Salient features that must be preserved:

  • Files of type 1 are more important than files of type 2; i.e. if a file of type 1 maps to a (key, value1) combination and a file of type 2 maps to a (key, value2) combination, then we add (key, value1) to cookieMap and not (key, value2). Edit: as pointed out by Bevan, this is not satisfied by my original procedural code.
  • Secondarily, CookieContainers with a higher Count have higher priority, i.e. if there are two (key, value) combos for the same key and both from the same filetype, we choose the one with higher value.Count.
  • Per-case exception handling is a must; screwing up a single file-reading should just allows us to note that and continue.

My best attempt started like this:

var cookieMap = (filesOfType1.Select(file => GetType1FileCookiePair(file))                 .Concat(filesOfType2.Select(file => GetType2FileCookiePair(file))))                     .GroupBy(pair => pair.Key)                     .Select(/* some way of selecting per the above bullets */)                     .ToDictionary(pair => pair.Key, pair => pair.Value); 

But it’s inelegant and filling in that comment block seems like a bitch. Right now I’m happy to stay procedural, but I thought that it might be a fun challenge to see if people can come up with something really clever.

  • 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. 2026-05-10T18:32:37+00:00Added an answer on May 10, 2026 at 6:32 pm

    Here’s my attempt – seemed simplest to split the task into three distinct statements.

    I’m using a helper function that returns null if the action throws an exception – for consistency with the answer from Omer van Kloeten, I’ve called this Swallow()

    Also, I’m not using the LINQ syntax, just the extension methods provided by System.Linq.Enumerable

    Lastly, note that this is uncompiled – so take it as intent.

    // Handle all files of type 1 var pairsOfType1 =      filesOfType1         .Select( file => Swallow( pairGetter(file)))         .Where( pair => pair != null);  // Handle files of type 2 and filter out those with keys already provided by type 1 var pairsOfType2 =     filesOfType2         .Select( file => Swallow( pairGetter(file)))         .Where( pair => pair != null);         .Where( pair => !pairsOfType1.Contains(p => p.Key == pair.Key));  // Merge the two sets, keeping only the pairs with the highest count var cookies =     pairsOfType1         .Union( pairsOfType2)         .GroupBy( pair => pair.Key)         .Select( group => group.OrderBy( pair => pair.Value.Count).Last());         .ToDictionary( pair => pair.Key); 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 98k
  • Answers 98k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Is E4X what you're looking for? It allows you to… May 11, 2026 at 7:38 pm
  • Editorial Team
    Editorial Team added an answer Found some quotes from COM Programming by Example: Another thing… May 11, 2026 at 7:38 pm
  • Editorial Team
    Editorial Team added an answer I find the Mozilla Developer Network very helpful. Steve May 11, 2026 at 7:38 pm

Related Questions

String s = ; for(i=0;i<....){ s = some Assignment; } or for(i=0;i<..){ String s
string percentage = e.Row.Cells[7].Text; I am trying to do some dynamic stuff with my
string [] files = new string[2]; files[0] = ThinkFarAhead.Example.Settings.Configuration_Local.xml; files[1] = ThinkFarAhead.Example.Settings.Configuration_Global.xml; //Resharper complains
string str1 = 12345ABC...\\...ABC100000; // Hypothetically huge string of 100000 + Unicode Chars str1

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.