Count same values in single array
-
how can i count same values considering i don't know what values are in the array? :confused:
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
-
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
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); }
-
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); }
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
-
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); }
-
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
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 typeint
.Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush
-
how can i count same values considering i don't know what values are in the array? :confused:
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]); }
-
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 typeint
.Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush
oops, youre right. i always run into this problem. guess LinkedList would fit here, too.
-
how can i count same values considering i don't know what values are in the array? :confused:
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.