Neat piped solution

  • 1

    One line extremely fast solution: translate spaces to returns with squeezing, sort the data, count appearances, sort in descending order and format as required by awk.

    # Read from the file words.txt and output the word frequency list to stdout.
    (tr -s ' ' '\n' < words.txt) | sort | uniq -c | sort -b -r -n | awk '{ print $2 " " $1 }'

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.