JS 依照元素出現數量排序

同樣是最近遇到的狀況,需要利用陣列內元素出現的數量進行排序,以下為範例程式:

var array = ["1", "2", "4", "5", "2", "4", "2", "3"];
var str = array.toString();
var filter = [];
var result = [];
document.write("原始陣列:" + array + "<br>");

var get = function (str, tar, arr, tmp) {
    if (str.indexOf(tar) >= 0) {
        tmp = str.slice(str.indexOf(tar) + 1);
        arr.push(tar);
        get(tmp, tar, arr, tmp);
    }
    return arr;
}

for (i in array) {
    var elm = [];
    var tmp;
    var fstr = filter.join();
    if (fstr.indexOf(array[i]) >= 0) {
        continue;
    } else {
        var tmp_arr = get(str, array[i], elm, tmp);
        result.push(tmp_arr.length + ':' + tmp_arr[0]);
        filter.push(array[i]);
    }
}

result.sort();
result.reverse();

//按照出現次數排序,不刪除重複元素
var finishArray = [];

for (index in result) {
    var show = result[index].split(':');
    for (i = 0; i < show[0]; i++) {
        finishArray.push(show[1]);
    }
}
document.write("按出現次數排序,不刪除重複元素:" + finishArray + "<br>");

//按照出現次數排序,刪除重複元素
var finishArray = [];
for (index in result) {
    var show = result[index].split(':');
    finishArray.push(show[1]);
}
document.write("按出現次數排序,刪除重複元素:" + finishArray + "<br>");

輸出結果:

原始陣列:1,2,4,5,2,4,2,3
按出現次數排序,不刪除重複元素:2,2,2,4,4,5,3,1
按出現次數排序,刪除重複元素:2,4,5,3,1

You may also like...

503,836 Responses

  1. Online poker offers many advantages. One of them is that there are
    no waiting lists, no second-hand smoke, and you can play any time you’d like.
    You can also enjoy many different games.

    Stop by my blog post :: dealers (incrediblepanels.com)

  2. 20tools.com表示:

    Betting on online sports betting gives bookmakers and bettors many benefits.

    Apart from allowing gamblers to place bets on their favorite sports online betting
    sites allow them to earn money. They provide a wide range
    of options for depositing money.

    My site: Casino – 20tools.com,

  3. Howdy! Would you mind if I share your blog with my facebook group?
    There’s a lot of folks that I think would really enjoy your content.
    Please let me know. Thank you

  4. Georgiana表示:

    The Internet has opened up a variety of new opportunities for people who love
    to play. The popularity of online poker has grown dramatically in recent years.

    Feel free to surf to my web-site; gaming (Georgiana)

  5. best表示:

    The Internet has created a wealth of new opportunities for those who love
    poker. The popularity of online poker has increased significantly in recent
    years.

    Also visit my webpage: best

  6. DavidLek表示:

    cvs prescription prices without insurance comfortis without vet prescription

  7. slots表示:

    Slots online are an increasingly popular pastime for millions of casino players online.
    This thrilling and enjoyable type of gambling is a excellent way to pass the time or
    to make extra money.

  8. can you mix viagra and sleeping pills – https://belviagra.com buy viagra online from uk

  9. DavidLek表示:

    mexican pharmacy without prescription how to get prescription drugs without doctor

  10. Sports表示:

    There are a myriad of rules and regulations that govern online gambling.
    For instance in the US, you cannot have gambling sites that are in your home country.
    However, it could be set up elsewhere.

    Feel free to visit my web-site: Sports

  11. blackjack表示:

    Online gambling has grown in popularity in the last decade.

    In 1996, there were just fifteen websites. In 1997, there were more than 200 websites.
    According to Frost & Sullivan, online gambling
    generated $830 million in revenues in 1998.

    Feel free to visit my page … blackjack

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。