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...

1,700,298 Responses

  1. Donaldabula表示:

    buy ed medication online fast pills easy online ed pharmacy

  2. Charlesbot表示:

    http://fastpillsformen.com/# Viagra generic over the counter

  3. RogerFaf表示:

    https://fastpillsformen.com/# Generic Viagra for sale

  4. RogerFaf表示:

    http://fastpillseasy.com/# buying ed pills online

  5. Bill Palmer表示:

    “The importance of proper disposal cannot be overstated—thank you, # any Keyword# junk car pick up Fort Myers FL

  6. What Are The Biggest “Myths” About Best Sex Machines Could Actually Be Accurate Sex Machine Buy

  7. You ought to take part in a contest for one of the finest websites on the web.

    I will highly recommend this site!

  8. website表示:

    Wonderful, what a blog it is! This web site gives valuable facts to
    us, keep it up.

  9. The Most Popular Ferrari Car Keys Is Gurus.
    Three Things Ferrari Aftermarket Key Fob Replacement; https://Minecraftcommand.Science,

  10. Marketing表示:

    5 Clarifications Regarding Ai Article Rewriter Marketing

  11. A Good Rant About Asbestos Attorney Mesothelioma lawsuits

  12. SLOT GACOR表示:

    I really like what you guys are up too. This kind
    of clever work and reporting! Keep up the fantastic works guys I’ve
    included you guys to my own blogroll.

  13. 9 . What Your Parents Teach You About Double Glazing Installer Near Me
    double glazing Installer Near me

  14. Donaldabula表示:

    ed meds by mail FastPillsEasy low cost ed meds online

  15. Patatapps.Fr表示:

    Why Butt Plug Online Store Is Fast Increasing To Be The Hot Trend For 2023 Butt Plug Play;
    Patatapps.Fr,

  16. Hello There. I discovered your weblog the usage of msn. This is a very smartly written article.

    I will make sure to bookmark it and come back to learn more
    of your useful info. Thank you for the post. I will certainly return.

    Also visit my webpage … Orion Depp Crypto Trading

  17. Charlesbot表示:

    https://fastpillsformen.com/# Order Viagra 50 mg online

  18. Mighty Doog Roofing
    Reimer Drive North 13768
    Maple Grove, MN 55311 United Ѕtates
    (763) 280-5115
    energy-efficient gutter cleaning near me

  19. Fantastic post however , I was wanting to know if you could write a litte more on this
    subject? I’d be very grateful if you could elaborate a little bit further.
    Many thanks!

  20. I am in fact glad to glance at this website posts which consists of plenty
    of helpful data, thanks for providing these kinds of information.

  21. Barrybib表示:

    Generic Cialis price: Generic Cialis without a doctor prescription – Generic Tadalafil 20mg price

  22. Charlesbot表示:

    https://fastpillseasy.com/# how to get ed pills

  23. I every time used to read article in news papers but now as I am a user of net
    thus from now I am using net for posts, thanks
    to web.

    Here is my site … Orion Depp LinkedIn

  24. Orion Depp表示:

    Thanks for every other informative website. Where else could I am getting that type of information written in such a perfect manner?
    I have a venture that I am just now working on, and I’ve been at the look out for such info.

    my web-site; Orion Depp

發佈回覆給「Donaldabula」的留言 取消回覆

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