By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,412 Members | 2,882 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,412 IT Pros & Developers. It's quick & easy.

function pointers

P: 8
how do you find the most frequent number in an array and count how many times it has occured using pointers?
Oct 9 '18 #1
Share this Question
Share on Google+
1 Reply

Expert Mod 5K+
P: 9,197
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.
Oct 9 '18 #2

Post your reply

Sign in to post your reply or Sign up for a free account.