LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-21-2006, 09:37 PM   #1
krock923
Member
 
Registered: Jul 2004
Posts: 171

Rep: Reputation: 30
Bash array question


Is there an easy way to get the most frequent item in an array? i.e. an array contains the following: 1,3,3,4,3,6,3 I want it to return 3.

Is there a better method than an array for doing this?
 
Old 06-21-2006, 10:02 PM   #2
krock923
Member
 
Registered: Jul 2004
Posts: 171

Original Poster
Rep: Reputation: 30
Eh, I have a feeling I'll end up just using perl. Thanks anyway.
 
Old 06-22-2006, 03:25 AM   #3
elluva
Member
 
Registered: Aug 2003
Location: Belguim, Ostend and Ghent
Distribution: Ubuntu
Posts: 600

Rep: Reputation: 30
For these types of things, I always write a small utility in C, it's way faster and easier to maintain.
 
Old 06-22-2006, 11:39 AM   #4
demon_vox
Member
 
Registered: May 2006
Location: Argentina
Distribution: SuSE 10
Posts: 173

Rep: Reputation: 30
Hi,
I think it can be done in Bash (bah, I know because I just did it jajaja). So here I present a solution:

Code:
function moreFrequent {

  nums=$*

  # Builds an array using your numbers as an index
  # and the frequency as its value.
  # In Bash it doesnt matter if the index are not contiguos
  for NUM in ${nums[@]}; do

    freq[$NUM]=`expr ${freq[$NUM]} + 1 `;

  done


  # The for writes a list with the number and its frequency
  # Then it is sorted by the frequency (with the sort command),
  # is filtered the first line online (with the head command),
  # and finaly just cuts the frequency part (with the cut command).
  for NUM in echo ${nums[@]}; do

    echo "$NUM ${freq[$NUM]}"

  done | sort -n -k 2 -r | head -n 1 | cut -d ' ' -f 1

}


numbers=(1 3 3 4 3 6 3 6 4 4)

yourCoolNumber=`moreFrequent ${numbers[@]}`

echo "The more frequent numbers is $yourCoolNumber"

Perhaps this is not the best solution, but it's a cool function which may be useful for you.

Hope it is!

Greetings.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Script Array index value Kedelfor Programming 10 04-29-2009 04:37 AM
Array Help in BASH! ?*%$ johnnybhoy67 Linux - Software 2 02-22-2006 10:39 AM
Bash files > array tigeraven Programming 3 12-13-2005 03:40 AM
Bash 2 dimensional array ldp Linux - General 5 07-29-2005 11:29 AM
array in bin/bash how to x2000koh Programming 12 05-09-2003 04:51 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration