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. avoid duplicates in list

avoid duplicates in list

Scheduled Pinned Locked Moved C#
tutorialquestion
7 Posts 5 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.
  • C Offline
    C Offline
    cst_kvp
    wrote on last edited by
    #1

    How to avoid duplicate items from the list? for example consider

    list contains 1,2,1,2,3,1,3,1,4,1,2,1,4

    actually i want only ones that item placed in list(1,2,3,4) others want to be removed. it is possible? then How?

    D G K 3 Replies Last reply
    0
    • C cst_kvp

      How to avoid duplicate items from the list? for example consider

      list contains 1,2,1,2,3,1,3,1,4,1,2,1,4

      actually i want only ones that item placed in list(1,2,3,4) others want to be removed. it is possible? then How?

      D Offline
      D Offline
      DaveyM69
      wrote on last edited by
      #2

      What type of list is it? The easiest way is not to allow duplicates in the first place when populating the list by using the Contains method if your list type has one (or similar).

      Dave

      C 1 Reply Last reply
      0
      • D DaveyM69

        What type of list is it? The easiest way is not to allow duplicates in the first place when populating the list by using the Contains method if your list type has one (or similar).

        Dave

        C Offline
        C Offline
        cst_kvp
        wrote on last edited by
        #3

        i have list[structure] named list[intpoint]. intpoint contains x and y values. i wants the idle values of x and y. not the repeated values. i want to remove the duplicated value of x and y in list[intpoint]. how it is possible?.

        M D 2 Replies Last reply
        0
        • C cst_kvp

          i have list[structure] named list[intpoint]. intpoint contains x and y values. i wants the idle values of x and y. not the repeated values. i want to remove the duplicated value of x and y in list[intpoint]. how it is possible?.

          M Offline
          M Offline
          Mbah Dhaim
          wrote on last edited by
          #4

          //let me assume you have object called IntPoint that store x and y values and list holder
          List pointList = new List();
          //when you want add to list check
          //let assume you have new IntPoint object called intPoint
          if (!pointList.Contains(intPoint))
          {
          pointList.Add(intPoint);
          }

          hope will help

          dhaim program is hobby that make some money as side effect :)

          1 Reply Last reply
          0
          • C cst_kvp

            i have list[structure] named list[intpoint]. intpoint contains x and y values. i wants the idle values of x and y. not the repeated values. i want to remove the duplicated value of x and y in list[intpoint]. how it is possible?.

            D Offline
            D Offline
            DaveyM69
            wrote on last edited by
            #5

            As I said avoid adding duplicates initially. This code will attempt to add Point(1,1) ten times but the list will only contain the one entry.

            List<Point> listPoint = new List<Point>();
            Point pointToAdd;
            pointToAdd = new Point(1, 1);
            for (int i = 0; i <= 10; i++)
            {
            if (!listPoint.Contains(pointToAdd))
            {
            listPoint.Add(pointToAdd);
            }
            }
            Console.WriteLine(listPoint.Count);

            If you really need to remove, I think the way is to create a new 'clean list' by iterating through the current list, checking before adding to the new list and then assigning the clean list to the old one.

            List<Point> CleanList(List<Point> listPoint)
            {
            List<Point> cleanList = new List<Point>();
            foreach (Point currentPoint in listPoint)
            {
            if (!(cleanList.Contains(currentPoint)))
            {
            cleanList.Add(currentPoint);
            }
            }
            return cleanList;
            }

            Then you can simply call listPoint = CleanList(listPoint);

            Dave

            1 Reply Last reply
            0
            • C cst_kvp

              How to avoid duplicate items from the list? for example consider

              list contains 1,2,1,2,3,1,3,1,4,1,2,1,4

              actually i want only ones that item placed in list(1,2,3,4) others want to be removed. it is possible? then How?

              G Offline
              G Offline
              Giorgi Dalakishvili
              wrote on last edited by
              #6

              If you are using .Net 3.5 you can use Enumerable.Distinct Method [^] Or HashSet<T> Generic Class[^]

              Giorgi Dalakishvili #region signature my articles #endregion

              1 Reply Last reply
              0
              • C cst_kvp

                How to avoid duplicate items from the list? for example consider

                list contains 1,2,1,2,3,1,3,1,4,1,2,1,4

                actually i want only ones that item placed in list(1,2,3,4) others want to be removed. it is possible? then How?

                K Offline
                K Offline
                Kevin McFarlane
                wrote on last edited by
                #7

                The free PowerCollections [^] library has methods for this kind of thing. It's very easy to use if you're already comfortable with generic collections (which you should be if you're using .NET 2 or higher).

                Kevin

                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