473,387 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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.
May 9 '07 #1
7 4530
Savage
1,764 Expert 1GB
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
May 9 '07 #2
JosAH
11,448 Expert 8TB
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
May 9 '07 #3
Savage
1,764 Expert 1GB
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.
May 9 '07 #4
sicarie
4,677 Expert Mod 4TB
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!
May 9 '07 #5
Savage
1,764 Expert 1GB
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
May 9 '07 #6
JosAH
11,448 Expert 8TB
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
May 9 '07 #7
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.
May 9 '07 #8

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

Similar topics

8
by: Wolfgang Lipp | last post by:
<annotation> the first eleven contributions in this thread started as an off-list email discussion; i have posted them here with the consent of their authors. -- _w.lipp </annotation> From:...
6
by: bosgoverde | last post by:
I've tried several ways to achieve a xsd schema for the following xml example, but failed to do so. Valid: <Person> <Interest>Movies</Interest> <Interest>Computers</Interest> </Person>...
7
by: Dung Ping | last post by:
Such as: <script> //code aaaa zzzz ccc
21
by: rccf6178 | last post by:
Hi all, Do anyone know how to write a C program to count the frequency of a certain key pattern in a given datafile? Input: • Three alpha-numeric keys separated by space • A data file...
19
by: jason_box | last post by:
I'm alittle new at C and I'm trying to write a simple program that will record the frequency of words and just print it out. It is suppose to take stdin and I heard it's only a few lines but I'm...
7
by: Udhay | last post by:
How to get the frequency of an audio file and how to separate the low and high frequency of an audio file
7
by: mcha226 | last post by:
Hi All I have to build a page which includes a select element (as a drop down menu) with all the country names in it. However I just found out that the need to be repeated many times so the...
5
by: Kefuci | last post by:
I've just got a question which is very hard for me to do. Here is the question: In the program you are supposed to take the elements of an array which can be composed of 20 elements at max.When the...
13
by: umpsumps | last post by:
Hello, Here is my code for a letter frequency counter. It seems bloated to me and any suggestions of what would be a better way (keep in my mind I'm a beginner) would be greatly appreciated.. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.