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. Count same values in single array

Count same values in single array

Scheduled Pinned Locked Moved C#
questiondata-structures
9 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.
  • C Offline
    C Offline
    Cptkli
    wrote on last edited by
    #1

    how can i count same values considering i don't know what values are in the array? :confused:

    P C L 3 Replies Last reply
    0
    • C Cptkli

      how can i count same values considering i don't know what values are in the array? :confused:

      P Offline
      P Offline
      pmarfleet
      wrote on last edited by
      #2

      Use a generic Dictionary to store the frequency count of each unique value in the array. Loop through the values in the array. For each value, increment the frequency count for that value in your Dictionary by 1. After you have passed through the array, your Dictionary should contain a number of items whose keys represent the unique values in your array. The value associated with each key will represent the frequency count for that value.

      Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

      C 1 Reply Last reply
      0
      • P pmarfleet

        Use a generic Dictionary to store the frequency count of each unique value in the array. Loop through the values in the array. For each value, increment the frequency count for that value in your Dictionary by 1. After you have passed through the array, your Dictionary should contain a number of items whose keys represent the unique values in your array. The value associated with each key will represent the frequency count for that value.

        Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

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

        Hi, sorry but how do you use Dictionary please? I can get unique values with the following code but i am stuck with the count. List myList = new List(); int index; for (int i = 0; i < myArray.Length; i++) { index = myList.BinarySearch(myArray[i]); if (index < 0) { myList.Insert(~index, myArray[i]); } } int[] arr = myList.ToArray(); foreach (Int32 tmp in arr) { Console.WriteLine("id: {0}", tmp); }

        B P 2 Replies Last reply
        0
        • C Cptkli

          Hi, sorry but how do you use Dictionary please? I can get unique values with the following code but i am stuck with the count. List myList = new List(); int index; for (int i = 0; i < myArray.Length; i++) { index = myList.BinarySearch(myArray[i]); if (index < 0) { myList.Insert(~index, myArray[i]); } } int[] arr = myList.ToArray(); foreach (Int32 tmp in arr) { Console.WriteLine("id: {0}", tmp); }

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

          i dont have any ide here, so i can only provide some untested half-pseudo-code. it might help you to get on the way.. map will contain your arrayObjects as key and the count as value. Hashmap map = new Hashmap(); for (int i=0; i

          P 1 Reply Last reply
          0
          • C Cptkli

            Hi, sorry but how do you use Dictionary please? I can get unique values with the following code but i am stuck with the count. List myList = new List(); int index; for (int i = 0; i < myArray.Length; i++) { index = myList.BinarySearch(myArray[i]); if (index < 0) { myList.Insert(~index, myArray[i]); } } int[] arr = myList.ToArray(); foreach (Int32 tmp in arr) { Console.WriteLine("id: {0}", tmp); }

            P Offline
            P Offline
            pmarfleet
            wrote on last edited by
            #5

            Did you try reading the documentation[^]?

            Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

            1 Reply Last reply
            0
            • B buchstaben

              i dont have any ide here, so i can only provide some untested half-pseudo-code. it might help you to get on the way.. map will contain your arrayObjects as key and the count as value. Hashmap map = new Hashmap(); for (int i=0; i

              P Offline
              P Offline
              pmarfleet
              wrote on last edited by
              #6

              Isn't Hashmap a Java type? I don't think it's available in the .NET framework. In this case, the OP should use the generic Dictionary type, with both the key and value of type int.

              Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

              B 1 Reply Last reply
              0
              • C Cptkli

                how can i count same values considering i don't know what values are in the array? :confused:

                C Offline
                C Offline
                Cptkli
                wrote on last edited by
                #7

                thanks got it working. Dictionary dic = new Dictionary(); for (int i = 0; i < myArray.Length; i++) { int value = myArray[i]; if (dic.ContainsKey(value)) { dic[value] += 1; } else { dic.Add(value, 1); } } int[] keys = new int[dic.Count]; int[] values = new int[dic.Count]; dic.Keys.CopyTo(keys, 0); dic.Values.CopyTo(values, 0); for (int i = 0; i < keys.Length; i++){ Console.WriteLine("{0} \t : {1}", keys[i], values[i]); }

                1 Reply Last reply
                0
                • P pmarfleet

                  Isn't Hashmap a Java type? I don't think it's available in the .NET framework. In this case, the OP should use the generic Dictionary type, with both the key and value of type int.

                  Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

                  B Offline
                  B Offline
                  buchstaben
                  wrote on last edited by
                  #8

                  oops, youre right. i always run into this problem. guess LinkedList would fit here, too.

                  1 Reply Last reply
                  0
                  • C Cptkli

                    how can i count same values considering i don't know what values are in the array? :confused:

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #9

                    Hi, the easiest approach I know is a two-step algorithm: 1. sort the array or whatever collection you have; .NET knows how to do that. 2. iterate over the collection, and count how often an item is identical to the previous one. BTW: if you are not allowed to modify (i.e. sort) the original collection, you will need a helper collection, such as a HashTable/Dictionary, as others already explained. :)

                    Luc Pattyn [Forum Guidelines] [My Articles]


                    This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


                    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