Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

Remove Duplicate Items (Using Delimitation)

If List1[0] = "Test|543543"
And List2[5] = "Test"

Then don't include it in FinalList.

Current code:

PHP Code

<?php 
List1 
= List1.Select(s => s.Split('|')[0]).Except(List2).ToList(); 

However, it only adds the delimited value to the list (Test), not the full value which I want (ex: Test|543543)

Can anyone help?
Here's a little program to remove dupes in c#

Code
       static void Main(string[] args)
       {
           var sr = new StreamReader(File.OpenRead(@"C:\Users\yourName\Desktop\dupes.txt")); //input
           var sw = new StreamWriter(File.OpenWrite(@"C:\Users\yourName\Desktop\clean.txt")); //output
           var lines = new HashSet<int>();
           while (!sr.EndOfStream)
           {
               string line = sr.ReadLine();
               int hc = line.GetHashCode();
               if (lines.Contains(hc))
                   continue;

               lines.Add(hc);
               sw.WriteLine(line);
           }
           sw.Flush();
           sw.Close();
           sr.Close();
       }
(May 30, 2022 07:51 AM)Focus Broken Wrote:  It's memorial day, remember to spit on a soldier's grave.
[Image: lrdgLIg.jpg]
(Apr 28, 2020 02:46 PM)Porn Wrote:  Here's a little program to remove dupes in c#

Code
       static void Main(string[] args)
       {
           var sr = new StreamReader(File.OpenRead(@"C:\Users\yourName\Desktop\dupes.txt")); //input
           var sw = new StreamWriter(File.OpenWrite(@"C:\Users\yourName\Desktop\clean.txt")); //output
           var lines = new HashSet<int>();
           while (!sr.EndOfStream)
           {
               string line = sr.ReadLine();
               int hc = line.GetHashCode();
               if (lines.Contains(hc))
                   continue;

               lines.Add(hc);
               sw.WriteLine(line);
           }
           sw.Flush();
           sw.Close();
           sr.Close();
       }

That isn't efficient enough, but thanks.

We're handling hundreds of thousands of items.

If anyone knows about LINQ and could modify my code it would be really appreciated!
You can use distinct buddy https://docs.microsoft.com/en-us/dotnet/...etcore-3.1
buy me a coffee eth:0x88621A52aa5Eb3b6503628369687111810186c58
(Apr 28, 2020 04:20 PM)itcorp Wrote:  You can use distinct buddy https://docs.microsoft.com/en-us/dotnet/...etcore-3.1

Distinct removes duplicates from a single list.

This will work:
Code
var final = list1.Where(x => !list2.Contains(x.Split('|').First()));
Lotta blood to let, peace to make, give a fuk about a check
(Apr 28, 2020 02:48 PM)draxyee Wrote:  That isn't efficient enough, but thanks.

We're handling hundreds of thousands of items.

If anyone knows about LINQ and could modify my code it would be really appreciated!

This will becoming slower as your collections grow. Have you considered a different data structure for what you are trying to achieve?
If you swap from list to dictionary you'll need to add some exception handling but it should handle large quantities significantly better.

Without seeing the data sample it's hard to comment but it might be worth doing a distinct on these if you start out with duplicates. I imagine you might be spending a bit of time in the split as well. If I were doing this on a large scale I'd probably save these individually so I don't need to perform the split.

It might also be worth breaking this down into set size lists. You could have different threads process the different list, it would be more work for the CPU but it might actually be faster if you can do more in parallel.

If you can supply the data (even if we are just talking a comparable test data) and we all have the same base to work from and the same output to achieve it could be a fun little experiment while working through any edge cases.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by dor343232Nov 13, 2019 02:56 AM 6 230 Nov 13, 2019 02:56 AM
Last Post: dor343232
[Python] by WoesMay 1, 2017 08:35 PM 9 324 May 1, 2017 08:35 PM
Last Post: Goodies
[.NET] by GørillaFeb 28, 2017 07:24 AM 13 451 Feb 28, 2017 07:24 AM
Last Post: Gørilla