Frequency of most repeated element from input.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • saidingist
    New Member
    • May 2007
    • 1

    Frequency of most repeated element from input.

    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.
  • Savage
    Recognized Expert Top Contributor
    • Feb 2007
    • 1759

    #2
    Originally posted by saidingist
    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.
    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.

    Savage

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by Savage
      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.

      Savage
      You 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

      Comment

      • Savage
        Recognized Expert Top Contributor
        • Feb 2007
        • 1759

        #4
        Originally posted by JosAH
        You 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
        Intresting.

        Thanks Jos.

        Comment

        • sicarie
          Recognized Expert Specialist
          • Nov 2006
          • 4677

          #5
          I'm just going to modify the thread title to better describe the issue - please let me know if you guys think it could be better!

          Comment

          • Savage
            Recognized Expert Top Contributor
            • Feb 2007
            • 1759

            #6
            Originally posted by sicarie
            I'm just going to modify the thread title to better describe the issue - please let me know if you guys think it could be better!
            Count the frequency of an inputed array element?

            Savage

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by sicarie
              I'm just going to modify the thread title to better describe the issue - please let me know if you guys think it could be better!
              I'd say the op wants the number with the maximum repetition count.

              kind regards,

              Jos

              Comment

              • bigturtle
                New Member
                • Apr 2007
                • 19

                #8
                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

                Working...