function pointers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ZS369
    New Member
    • Aug 2018
    • 8

    function pointers

    how do you find the most frequent number in an array and count how many times it has occured using pointers?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    I would use mapping arrays.

    One array for the values and a second array for the number of occurrences.


    Initialize the values array with a number for each element that means "not used".

    Initialize the values array setting each element to 0.

    The you march down to your test array once.

    For each element in the test array, look up the element value in your values array. If it's not there then put the value in one of the "not used" elements.


    Add 1 to the number of occurrences for that value.


    Remember, the third element in your values array uses the third total in your occurrences array.

    At end of your pass of the test array, all the values and occurrences are in your mapping arrays.

    Now just look for the largest number in your occurrences array. When you find it, the number it represents will be at the same position in your values array.

    Comment

    Working...