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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T17:59:17+00:00 2026-06-03T17:59:17+00:00

I have the following code to optimize. As I expect the file to be

  • 0

I have the following code to optimize. As I expect the file to be large, I did not use a HashMap to store the lines but opted instead for a String array. I tried testing the logic with n of about 500,000 and it ran for approximately 14 minutes. I would definitely like to make it a lot faster than that and would appreciate any help or suggestions.

         public static void RemoveDuplicateEntriesinFile(string filepath)
        {
              if (filepath == null)
                    throw new ArgumentException("Please provide a valid FilePath");
              String[] lines = File.ReadAllLines(filepath);
              for (int i = 0; i < lines.Length; i++)
              {
                    for (int j = (i + 1); j < lines.Length; j++)
                    {
                          if ((lines[i] !=null) && (lines[j]!=null) && lines[i].Equals(lines[j]))
                          {//replace duplicates with null
                                lines[j] = null;
                          }
                    }
              }

              File.WriteAllLines(filepath, lines);
        }

Thanks in Advance!

  • 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-03T17:59:27+00:00Added an answer on June 3, 2026 at 5:59 pm

    “As I expect the file to be large, I did not use a HashMap to store the lines but opted instead for a String array.”

    I don’t agree with your reasoning; the larger the file, the more of a performance benefit you’ll get from hashing. In your code, you’re comparing each lines with all succeeding lines, requiring O(n²) computational complexity for the whole file.

    On the other hand, if you were to use an efficient hashing algorithm, then each hash lookup would complete in O(1); the computational complexity of processing your entire file becomes O(n).

    Try using a HashSet<string> and observe the difference in the processing time:

    public static void RemoveDuplicateEntriesinFile(string filepath)
    {
        if (filepath == null)
            throw new ArgumentException("Please provide a valid FilePath");
    
        HashSet<string> hashSet = new HashSet<string>(File.ReadLines(filepath));
        File.WriteAllLines(filepath, hashSet);
    }
    

    Edit: Could you try the following version of the algorithm and check how long it takes? It’s optimized to minimize memory consumption:

    HashAlgorithm hashAlgorithm = new SHA256Managed();
    HashSet<string> hashSet = new HashSet<string>();
    string tempFilePath = filepath + ".tmp";
    
    using (var fs = new FileStream(tempFilePath, FileMode.Create, FileAccess.Write))
    using (var sw = new StreamWriter(fs))
    {
        foreach (string line in File.ReadLines(filepath))
        {
            byte[] lineBytes = Encoding.UTF8.GetBytes(line);
            byte[] hashBytes = hashAlgorithm.ComputeHash(lineBytes);
            string hash = Convert.ToBase64String(hashBytes);
    
            if (hashSet.Add(hash))
                sw.WriteLine(line);
        }
    }
    
    File.Delete(filepath);
    File.Move(tempFilePath, filepath);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code snippet that i use to compile class at the run
I have the following code that I use to compute the distance between two
I have the following code in Python: def point_to_index(point): if point not in points:
I have following Code Block Which I tried to optimize in the Optimized section
I have the following code that I use to reload data in a grid.
I have the following piece of code which I'd like to optimize using Cython:
I have following code in my application: // to set tip - photo in
I have following code in my Application. Comments in my code will specify My
I have following code in my application. [self.navigationController pushViewController:x animated:YES]; It will push a
I have following code class User attr_accessor :name end u = User.new u.name =

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.