Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. {Solved} Find and count duplicates in a list

{Solved} Find and count duplicates in a list

Scheduled Pinned Locked Moved C#
regexquestionvisual-studiohelptutorial
10 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Acuena
    wrote on last edited by
    #1

    Hi all! Got a new question. I have a barcode scanner that saves all scanned barcodes in the following format: Date,Time,Barcode Example:

    2020-07-08,13:54:16,1578456329781
    2020-07-08,14:07:13,1578453697235
    2020-07-08,14:08:03,1548795358155
    2020-07-08,14:11:09,1578453697235
    2020-07-08,14:11:42,1578453697235

    Now I add this to a ListView in Gridview mode with the following code:

    List items = new List();
    string[] lines = File.ReadAllLines(@"F:\BARCODE.txt");

    foreach (string line in lines)
    {
    string Pattern = @"(?\d{4}-\d{2}-\d{2})(?:,)(?\d{2}:\d{2}:\d{2})(?:,)(?\d*)";
    Regex R = new Regex(Pattern);
    Match m = R.Match(line);
    items.Add(new List() { Date = m.Groups["Date"].ToString(),
    Time = m.Groups["Time"].ToString(), EAN = m.Groups["EAN"].ToString() });
    }
    LvItems.ItemsSource = items;

    Now I want to be able to count if there are any duplicates, and show the results in a new ListView like this: (| shows new Column in the gridview)

    1578456329781|1
    1578453697235|3
    1548795358155|1

    Does anyone have a better idea than using a atleast two loops to do this? I was thinking on using one loop to go threw the list, and then use the second loop to check for duplicates and count. Wondering if there are a simplier solution for this. Any help, as always, are much appreciated.

    Have a nice day! Have gotten a new IDE :)

    OriginalGriffO A B 3 Replies Last reply
    0
    • A Acuena

      Hi all! Got a new question. I have a barcode scanner that saves all scanned barcodes in the following format: Date,Time,Barcode Example:

      2020-07-08,13:54:16,1578456329781
      2020-07-08,14:07:13,1578453697235
      2020-07-08,14:08:03,1548795358155
      2020-07-08,14:11:09,1578453697235
      2020-07-08,14:11:42,1578453697235

      Now I add this to a ListView in Gridview mode with the following code:

      List items = new List();
      string[] lines = File.ReadAllLines(@"F:\BARCODE.txt");

      foreach (string line in lines)
      {
      string Pattern = @"(?\d{4}-\d{2}-\d{2})(?:,)(?\d{2}:\d{2}:\d{2})(?:,)(?\d*)";
      Regex R = new Regex(Pattern);
      Match m = R.Match(line);
      items.Add(new List() { Date = m.Groups["Date"].ToString(),
      Time = m.Groups["Time"].ToString(), EAN = m.Groups["EAN"].ToString() });
      }
      LvItems.ItemsSource = items;

      Now I want to be able to count if there are any duplicates, and show the results in a new ListView like this: (| shows new Column in the gridview)

      1578456329781|1
      1578453697235|3
      1548795358155|1

      Does anyone have a better idea than using a atleast two loops to do this? I was thinking on using one loop to go threw the list, and then use the second loop to check for duplicates and count. Wondering if there are a simplier solution for this. Any help, as always, are much appreciated.

      Have a nice day! Have gotten a new IDE :)

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      For starters, I wouldn't use a Regex: use string.Split instead:

      string[] lines = File.ReadAllLines(filePath);
      var barcodes = lines.Select(line => {string[] parts = line.Split(',');
      return parts[2]; });

      You can then use GroupBy to combine them and get the Key - the barcode number - and it's Count:

      var x = barcodes.GroupBy(b => b);

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      A 1 Reply Last reply
      0
      • A Acuena

        Hi all! Got a new question. I have a barcode scanner that saves all scanned barcodes in the following format: Date,Time,Barcode Example:

        2020-07-08,13:54:16,1578456329781
        2020-07-08,14:07:13,1578453697235
        2020-07-08,14:08:03,1548795358155
        2020-07-08,14:11:09,1578453697235
        2020-07-08,14:11:42,1578453697235

        Now I add this to a ListView in Gridview mode with the following code:

        List items = new List();
        string[] lines = File.ReadAllLines(@"F:\BARCODE.txt");

        foreach (string line in lines)
        {
        string Pattern = @"(?\d{4}-\d{2}-\d{2})(?:,)(?\d{2}:\d{2}:\d{2})(?:,)(?\d*)";
        Regex R = new Regex(Pattern);
        Match m = R.Match(line);
        items.Add(new List() { Date = m.Groups["Date"].ToString(),
        Time = m.Groups["Time"].ToString(), EAN = m.Groups["EAN"].ToString() });
        }
        LvItems.ItemsSource = items;

        Now I want to be able to count if there are any duplicates, and show the results in a new ListView like this: (| shows new Column in the gridview)

        1578456329781|1
        1578453697235|3
        1548795358155|1

        Does anyone have a better idea than using a atleast two loops to do this? I was thinking on using one loop to go threw the list, and then use the second loop to check for duplicates and count. Wondering if there are a simplier solution for this. Any help, as always, are much appreciated.

        Have a nice day! Have gotten a new IDE :)

        A Offline
        A Offline
        Afzaal Ahmad Zeeshan
        wrote on last edited by
        #3

        Quote:

        I was thinking on using one loop to go threw the list, and then use the second loop to check for duplicates and count.

        You are right, it is not just about finding/removing the duplicates, but also counting them. For a custom solution, you can scan the list and create a Dictionary that holds the times that a key occurs; check if the key exists, then increment the value, otherwise, add the key with 1 as value. The algorithm will run O(N) in a single go, and you can then traverse on the Dictionary while performing your actions—and this part depends on how you use the Dictionary and what purpose you make it serve. You will be using a single loop, and the front-end framework (ListView) would then be able to render the results itself; which will again be a loop. You might as well want to use LINQ or other "shiny" C# features to perform this as well, [C# LINQ find duplicates in List - Stack Overflow](https://stackoverflow.com/questions/18547354/c-sharp-linq-find-duplicates-in-list)

        The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~

        1 Reply Last reply
        0
        • A Acuena

          Hi all! Got a new question. I have a barcode scanner that saves all scanned barcodes in the following format: Date,Time,Barcode Example:

          2020-07-08,13:54:16,1578456329781
          2020-07-08,14:07:13,1578453697235
          2020-07-08,14:08:03,1548795358155
          2020-07-08,14:11:09,1578453697235
          2020-07-08,14:11:42,1578453697235

          Now I add this to a ListView in Gridview mode with the following code:

          List items = new List();
          string[] lines = File.ReadAllLines(@"F:\BARCODE.txt");

          foreach (string line in lines)
          {
          string Pattern = @"(?\d{4}-\d{2}-\d{2})(?:,)(?\d{2}:\d{2}:\d{2})(?:,)(?\d*)";
          Regex R = new Regex(Pattern);
          Match m = R.Match(line);
          items.Add(new List() { Date = m.Groups["Date"].ToString(),
          Time = m.Groups["Time"].ToString(), EAN = m.Groups["EAN"].ToString() });
          }
          LvItems.ItemsSource = items;

          Now I want to be able to count if there are any duplicates, and show the results in a new ListView like this: (| shows new Column in the gridview)

          1578456329781|1
          1578453697235|3
          1548795358155|1

          Does anyone have a better idea than using a atleast two loops to do this? I was thinking on using one loop to go threw the list, and then use the second loop to check for duplicates and count. Wondering if there are a simplier solution for this. Any help, as always, are much appreciated.

          Have a nice day! Have gotten a new IDE :)

          B Offline
          B Offline
          BillWoodruff
          wrote on last edited by
          #4

          Finding duplicates by barcodes is easy; i'd suggest you get your data into an "object" where it will be easy to perform any type of query on it: I'd use a Dictionary:

          private readonly string bcdata = @"2020-07-08,13:54:16,1578456329781
          2020-07-08,14:07:13,1578453697235
          2020-07-08,14:08:03,1548795358155
          2020-07-08,14:11:09,1578453697235
          2020-07-08,14:11:42,1578453697235";

          private char[] splitwhtspc = {'\r', '\n', ' ', '\t'};
          private char[] splitcomma = {','};

          // in some method or event-handler body:

          var lines = bcdata.Split(splitwhtspc, StringSplitOptions.RemoveEmptyEntries);

          Dictionary dttobc = lines.Select
          (
          line => line.Split(splitcomma, StringSplitOptions.RemoveEmptyEntries)
          ).Select
          (
          sary =>
          new
          {
          key = DateTime.Parse(sary[0] + " " + sary[1]),
          value = long.Parse(sary[2])
          }
          )
          .ToDictionary(
          kl => kl.key,
          vl => vl.value
          );

          Using this Dictionary, you can easily create a new Dictionary of barcodes and number of barcodes:

          Dictionary dups = dttobc.Values.GroupBy(v => v).ToDictionary(g => g.Key, g => g.Count());

          «One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali

          1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            For starters, I wouldn't use a Regex: use string.Split instead:

            string[] lines = File.ReadAllLines(filePath);
            var barcodes = lines.Select(line => {string[] parts = line.Split(',');
            return parts[2]; });

            You can then use GroupBy to combine them and get the Key - the barcode number - and it's Count:

            var x = barcodes.GroupBy(b => b);

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

            A Offline
            A Offline
            Acuena
            wrote on last edited by
            #5

            Hi! Sorry for late reply, been a bit busy. Anyhow, I have tried your example, but how can I output it to a Listview? I have the following code now:

            string[] lines = File.ReadAllLines(@"C:\Users\\Desktop\Test data.txt");
            var barcodes = lines.Select(line => {string[] parts = line.Split(',');
            return parts[2]; });
            var x = barcodes.GroupBy(b => b);

            Tried differnt mthoods, among other: Messagebox.Show(x.ToString()); That failed :P Thankfull for any help.

            Have a nice day! I have a new IDE!

            OriginalGriffO 1 Reply Last reply
            0
            • A Acuena

              Hi! Sorry for late reply, been a bit busy. Anyhow, I have tried your example, but how can I output it to a Listview? I have the following code now:

              string[] lines = File.ReadAllLines(@"C:\Users\\Desktop\Test data.txt");
              var barcodes = lines.Select(line => {string[] parts = line.Split(',');
              return parts[2]; });
              var x = barcodes.GroupBy(b => b);

              Tried differnt mthoods, among other: Messagebox.Show(x.ToString()); That failed :P Thankfull for any help.

              Have a nice day! I have a new IDE!

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #6

              What does GroupBy return? What type is x? Why would you assume that ToString would do anything useful?

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              A 1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                What does GroupBy return? What type is x? Why would you assume that ToString would do anything useful?

                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

                A Offline
                A Offline
                Acuena
                wrote on last edited by
                #7

                I have never used LinQ, and as such I am a bit unsure what GroupBy returns and what type x is. And about the ToString, I was just testing, all (or many) does... Here is the whole code I have, if anyone is interested:

                using System;
                using System.Collections.Generic;
                using System.Windows;
                using System.IO;
                using System.Linq;

                namespace Inventory
                {
                /// /// Interaction logic for Window1.xaml
                ///
                public partial class Window1 : Window
                {
                void button1_Click(object sender, RoutedEventArgs e)
                {

                		string\[\] lines = File.ReadAllLines(@"C:\\Users\\sdadmin\\Desktop\\Test data.txt");
                		var barcodes = lines.Select(line => {string\[\] parts = line.Split(',');
                		                            	return parts\[2\]; });
                		var x = barcodes.GroupBy(b => b);		
                	}
                	
                	public class List
                	{
                		public string Date{get; set;}
                		public string Time{get; set;}
                		public string EAN {get; set;}
                	}
                }
                

                }

                I am pretty new to C#, I have only been at it for about 1 month or so, if I disregard time when I don't do any programming...

                Have a nice day!

                OriginalGriffO 1 Reply Last reply
                0
                • A Acuena

                  I have never used LinQ, and as such I am a bit unsure what GroupBy returns and what type x is. And about the ToString, I was just testing, all (or many) does... Here is the whole code I have, if anyone is interested:

                  using System;
                  using System.Collections.Generic;
                  using System.Windows;
                  using System.IO;
                  using System.Linq;

                  namespace Inventory
                  {
                  /// /// Interaction logic for Window1.xaml
                  ///
                  public partial class Window1 : Window
                  {
                  void button1_Click(object sender, RoutedEventArgs e)
                  {

                  		string\[\] lines = File.ReadAllLines(@"C:\\Users\\sdadmin\\Desktop\\Test data.txt");
                  		var barcodes = lines.Select(line => {string\[\] parts = line.Split(',');
                  		                            	return parts\[2\]; });
                  		var x = barcodes.GroupBy(b => b);		
                  	}
                  	
                  	public class List
                  	{
                  		public string Date{get; set;}
                  		public string Time{get; set;}
                  		public string EAN {get; set;}
                  	}
                  }
                  

                  }

                  I am pretty new to C#, I have only been at it for about 1 month or so, if I disregard time when I don't do any programming...

                  Have a nice day!

                  OriginalGriffO Offline
                  OriginalGriffO Offline
                  OriginalGriff
                  wrote on last edited by
                  #8

                  If you don't know what type something is, you can hover the mouse over it, and VS will tell you. In this case it's a IEnumerable<IGrouping<string, string>> Which means that it's a collection of Groups of string key / string value pairs. Each Group in the collection has two properties you are interested in: the Key (which is the barcode number) and the Count method which tells you how many items are in each group. So it's pretty simple to access the barcode and count:

                          string\[\] lines = File.ReadAllLines(filePath);
                          var barcodes = lines.Select(line => {
                              string\[\] parts = line.Split(',');
                              return parts\[2\];
                          });
                          var x = barcodes.GroupBy(b => b);
                          foreach (var y in x)
                              {
                              Console.WriteLine($"{y.Key} : {y.Count()}");
                              }
                  

                  For your sample data you get:

                  1578456329781 : 1
                  1578453697235 : 3
                  1548795358155 : 1

                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                  "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                  A 1 Reply Last reply
                  0
                  • OriginalGriffO OriginalGriff

                    If you don't know what type something is, you can hover the mouse over it, and VS will tell you. In this case it's a IEnumerable<IGrouping<string, string>> Which means that it's a collection of Groups of string key / string value pairs. Each Group in the collection has two properties you are interested in: the Key (which is the barcode number) and the Count method which tells you how many items are in each group. So it's pretty simple to access the barcode and count:

                            string\[\] lines = File.ReadAllLines(filePath);
                            var barcodes = lines.Select(line => {
                                string\[\] parts = line.Split(',');
                                return parts\[2\];
                            });
                            var x = barcodes.GroupBy(b => b);
                            foreach (var y in x)
                                {
                                Console.WriteLine($"{y.Key} : {y.Count()}");
                                }
                    

                    For your sample data you get:

                    1578456329781 : 1
                    1578453697235 : 3
                    1548795358155 : 1

                    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

                    A Offline
                    A Offline
                    Acuena
                    wrote on last edited by
                    #9

                    Thanks for the help! That worked like a charm. Was unfamiliar that you could hover the mouse over the variable and the IDE would tell you what type it was. On the PC I am on now, at work, I can't install a newer framework, so I had to redo the code to not use String interpolation, sadly. Also I am using SharpDevlop. Anyhow, thanks for the help :)

                    Have a nice day!

                    OriginalGriffO 1 Reply Last reply
                    0
                    • A Acuena

                      Thanks for the help! That worked like a charm. Was unfamiliar that you could hover the mouse over the variable and the IDE would tell you what type it was. On the PC I am on now, at work, I can't install a newer framework, so I had to redo the code to not use String interpolation, sadly. Also I am using SharpDevlop. Anyhow, thanks for the help :)

                      Have a nice day!

                      OriginalGriffO Offline
                      OriginalGriffO Offline
                      OriginalGriff
                      wrote on last edited by
                      #10

                      You're welcome!

                      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

                      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups