I'm struggling with writing a code for a project MODE. The program should find the number in an array which repeats the most. For example if you've entered a set of 10 numbers like 1 2 1 2 4 2 5 2 6 9 the program should say the number 2 repeats 4 times. I appreciate your help with any idias. Thank you.
Frequency of most repeated element from input.
Collapse
X
-
Tags: None
-
Find the first number in array and set it us a referance value.Search trough array and if any other number is equal to the referenced increase the value,which shows how many of those elements are in array.After this output it to another array which max size is size of the first array for the first dimenison and secound dimension points at the number.Reapet this for secound and all other numbers in array.Then sort the secound array in descanding order and first element then is the with index 0.Originally posted by saidingistI'm struggling with writing a code for a project MODE. The program should find the number in an array which repeats the most. For example if you've entered a set of 10 numbers like 1 2 1 2 4 2 5 2 6 9 the program should say the number 2 repeats 4 times. I appreciate your help with any idias. Thank you.
Savage -
You don't need to iterate over the first array all the time: just sort it; next, in oneOriginally posted by SavageFind the first number in array and set it us a referance value.Search trough array and if any other number is equal to the referenced increase the value,which shows how many of those elements are in array.After this output it to another array which max size is size of the first array for the first dimenison and secound dimension points at the number.Reapet this for secound and all other numbers in array.Then sort the secound array in descanding order and first element then is the with index 0.
Savage
sweep you can find the largest repetition count. That reduces the big-Oh from
O(n^2) (your method) to O(n*log(n)).
kind regards,
JosComment
-
Intresting.Originally posted by JosAHYou don't need to iterate over the first array all the time: just sort it; next, in one
sweep you can find the largest repetition count. That reduces the big-Oh from
O(n^2) (your method) to O(n*log(n)).
kind regards,
Jos
Thanks Jos.Comment
-
If the range of values is small, e.g. 0-10 as in your example, you can solve your problem with a single pass through the array. Set up a counting array NrA[] of integers, with one element for each possible value in your original array A[]. Zero all elements of NrA. Then go through A and for each element, say V = A[J], add 1 to NrA[V], the number of occurrences of value V. When you're finished, the counting array will contain, for each V, the # of times V occurs as a value in A.
Suppose your initial array is A[0..9] = 1 2 1 2 4 2 5 2 6 9 as in original post, then when you're finished, your counting array will be NrA[0..10] = 0 2 4 0 1 1 1 0 0 1 0. E.g., NrA[2] = 4 since A contains 4 elements equal to 2, and in gerneral NrA[J] = the number of J's in A. The most frequently occurring element is the index of the largest value in NrA, namely 2 in this case since NrA[2} is the max value in NrA.
This method works in linear time O(n+m), where n is the number of elements and m is the number of possible values. It's the fastest possible sorting method in cases where the range of values is small enough, since it involves no comparisons at all and looks at each element exactly once.Comment
Comment