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,365,215 Responses

  1. If you are looking to buy a verified Cash App account, it’s important to consider the benefits.

    Verified accounts offer increased security, higher transaction limits, and
    access to additional features. However, it’s crucial to find reliable sellers to avoid
    scams or fraudulent accounts. Take the time to research and verify the legitimacy
    of the seller before making any purchase. Remember,
    a verified Cash App account can provide a seamless and secure payment experience.

  2. Chas表示:

    famous handbag designers (Chas)

  3. JuliusMox表示:

    https://kamagra.team/# Kamagra tablets

  4. Denice表示:

    asbestos mesothelioma attorneys (Denice)

  5. %%

    my web site; Part Time Work From Home Jobs (http://Sj.Fineweb.Co.Kr/)

  6. railway cancer concession form [motogpdb.Racing]

  7. attorney表示:

    %%

    My homepage – attorney

  8. %%

    Also visit my web-site; Bmw Key (https://Click4R.Com)

  9. Sherrie表示:

    mesothelioma lawsuit kentucky; Sherrie,

  10. I think that everything composed was actually very logical.
    However, consider this, what if you added a little information? I
    am not saying your information isn’t good., however what if you added something that makes people want more?

    I mean JS 依照元素出現數量排序 –
    馬老師 雲端研究室 is a little plain. You might look at Yahoo’s
    home page and see how they create news headlines
    to get people to open the links. You might add a related video or a related picture or two
    to get people interested about everything’ve written. Just my opinion, it might bring your blog a little livelier.

  11. Keto Max Pro表示:

    %%

    Check out my homepage – Keto Max Pro

  12. P3terx.com表示:

    %%

    Feel free to visit my homepage – toyota Replacement
    key fob (P3terx.com)

  13. I enjoy what you guys tend to be up too. This kind of clever work and exposure!
    Keep up the wonderful works guys I’ve included you guys to our blogroll.

  14. %%

    Feel free to surf to my site … mercedes Replacement Key

  15. I have read so many content about the blogger lovers
    but this article is actually a fastidious piece of writing, keep
    it up.

  16. Buying a verified Cash App account comes with many benefits.

    With a verified account, you can enjoy a higher transaction limit,
    enhanced security features, and access to exclusive Cash
    App features. Don’t miss out on the advantages – get a verified Cash App account today!

發佈留言

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