题解 | #shell/bash统计每个单词出现的个数#

第二列是否有重复

http://www.nowcoder.com/practice/61b79ffe88964c7ab7b98ae16dd76492

偷懒,输出前少一步排序

# cat nowcoder.txt | awk '{print $2}' | sort -r | uniq -cd
# awk '{print $2}' nowcoder.txt | sort -r | uniq -cd
# cut -d" " -f2 nowcoder.txt | sort -r| uniq -cd
declare -A map 
function foreach() {
    local tem=""
    for key in ${!map[@]}; do
        tem=${map[${key}]}
        if [ ${tem} -gt 1 ]; then
            echo "${tem} ${key}"
        fi
    done
}

function test2() {
    local temp=""
    for line in `cut -d" " -f2 nowcoder.txt`; do
        temp=${map[${line}]}
        if [ -z "${temp}" ]; then
            map[${line}]=1
        else 
            ((map[${line}]++))
        fi
    done
    foreach
}

function test3() {
    local ele=""
    local arr=""
    while read line; do
        arr=(${line})
        ele=${arr[1]}
        temp=${map[${ele}]}
        if [ -z "${temp}" ]; then
            map[${ele}]=1
        else 
            ((map[${ele}]++))
        fi
    done < nowcoder.txt
    foreach
}

# test2
test3
全部评论
这么离谱
点赞 回复 分享
发布于 2022-06-30 14:45

相关推荐

不愿透露姓名的神秘牛友
07-02 15:39
点赞 评论 收藏
分享
认真搞学习:这么良心的老板真少见
点赞 评论 收藏
分享
后来123321:别着急,我学院本大二,投了1100份,两个面试,其中一个还是我去线下招聘会投的简历,有时候这东西也得看运气
无实习如何秋招上岸
点赞 评论 收藏
分享
07-02 10:44
门头沟学院 C++
码农索隆:太实诚了,告诉hr,你能实习至少6个月
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务