304 North Cardinal St.
Dorchester Center, MA 02124

# Word Frequency LeetCode Solution

## Problem – Word Frequency LeetCode Solution

Write a bash script to calculate the frequency of each word in a text file `words.txt`.

For simplicity sake, you may assume:

• `words.txt` contains only lowercase characters and space `' '` characters.
• Each word must consist of lowercase characters only.
• Words are separated by one or more whitespace characters.

Example:

Assume that `words.txt` has the following content:

``````the day is sunny the the
the sunny is is
``````

Your script should output the following, sorted by descending frequency:

``````the 4
is 3
sunny 2
day 1
``````

Note:

• Don’t worry about handling ties, it is guaranteed that each word’s frequency count is unique.
• Could you write it in one-line using Unix pipes?

## Word Frequency LeetCode Solution in Bash

``````declare -A arr #associative array

do
for word in \$line
do
let arr[\$word]=\${arr[\$word]}+1
done
done < words.txt

for key in \${!arr[@]}
do
echo \$key \${arr[\$key]}
done | sort -rn -k2
``````
##### Word Frequency LeetCode Solution Review:

In our experience, we suggest you solve this Word Frequency LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Word Frequency LeetCode Solution

Find on Leetcode

##### Conclusion:

I hope this Word Frequency LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions