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,383,622 Responses

  1. my site is https://www.igfitalia.org/. feel free to visit. it is all about mege888 megi888 mego888 megu888.my site not a scam. my site really not a scam. my site confirm not a scam.

  2. program表示:

    %%

    Feel free to surf to my web page … program

  3. Thank you, I value this!

  4. Hmm is anyone else having problems with the images on this blog loading?
    I’m trying to find out if its a problem on my end or if
    it’s the blog. Any feedback would be greatly appreciated.

  5. Looking to buy a verified Cash App account? Look no further!
    A verified Cash App account provides added security and peace of mind for your financial transactions.
    Don’t compromise on safety, invest in a verified account today!

  6. What’s up Dear, are you genuinely visiting this web page on a regular basis, if
    so then you will without doubt obtain nice knowledge.

  7. Cyrus表示:

    %%

    my blog :: replacement key for saab 93 (Cyrus)

  8. Looking for a hassle-free way to buy a verified Cash App account?
    Look no further! With a verified account, you can enjoy enhanced security, increased transaction limits, and added convenience.
    Don’t waste time, get your verified Cash App account today and start
    enjoying seamless money transfers!

  9. Very energetic blog, I loved that bit. Will there be
    a part 2?

  10. Ezra Gainey表示:

    https://accounting11.b-cdn.net/research/accounting11-(123).html
    Stick to a small yet stately earring and a cocktail ring, and hold extra sparkle to a minimal.

  11. Looking to buy a verified Cash App account? Look no further!

    Purchasing a verified account ensures safe and secure transactions, higher spending limits,
    and access to various features. Save time and
    effort by getting a verified Cash App account today! Enhance
    your financial convenience with a trustworthy and reliable platform.
    Don’t miss out on this opportunity.

  12. It’s an remarkable article in favor of all the web viewers; they will obtain benefit from it I am sure.

  13. RichardLak表示:

    Levitra online USA fast п»їLevitra price Cheap Levitra online

  14. Buying a verified Cash App account offers numerous advantages.
    With a verified account, you can enjoy increased transaction limits, better
    security features, and access to additional functionalities.
    It also provides peace of mind as verification implies a thorough background check.
    So, if you want to make the most of your Cash App experience, consider investing in a verified account
    today.

  15. Leslee表示:

    %%

    Check out my blog; renault trafic key replacement; Leslee,

  16. doğum haritası hesaplama,Nisan 2022 Burç Yorumları

  17. If you’re looking to buy a verified Cash App account, you’ve come to the right place.
    A verified Cash App account offers added security and more features, making it a wise investment.
    Don’t risk your money and personal information with unverified accounts –
    get a credible one today!

  18. Hi colleagues, its enormous article on the topic of tutoringand fully defined, keep it up all the time.

  19. Dann表示:

    %%

    my page: volvo v70 key (Dann)

  20. Online Casino表示:

    Howdy! Do you know if they make any plugins to protect against hackers?

    I’m kinda paranoid about losing everything I’ve worked hard on. Any tips?

  21. Beatrice表示:

    %%

    Also visit my homepage: bmw replacement keys; Beatrice,

  22. %%

    My web page :: asbestos mesothelioma claim (comedycoup.com)

  23. Katlyn表示:

    asbestosis asbestos mesothelioma attorney (Katlyn)

  24. Hi there, I log on to your blog like every week.

    Your writing style is awesome, keep doing what you’re doing!

  25. 11 “Faux Pas” That Are Actually Acceptable To Create With Your wasatch
    Railroad contractors lawsuit (sanford-Wilder.hubstack.net) Lawsuit
    Mds

發佈留言

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