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,244,869 Responses

  1. 21112024AB2表示:

    Wonderful items from you, man. I have be mindful your stuff
    prior to and you’re simply too great. I actually like what
    you’ve got here, really like what you’re stating and the way in which through
    which you assert it. You are making it entertaining and you continue to take care of to stay it sensible.
    I can’t wait to learn far more from you. That is actually
    a terrific web site.

  2. JacobLoody表示:

    goldcoon.ru/ – Профессиональные решения для вашей кухни.

  3. вывод из запоя на дому краснодар круглосуточно http://nsk.ukrbb.net/viewtopic.php?f=45&t=29670 .

  4. DarrenUnjuh表示:

    http://lisinopril1st.com/# lisinopril1st

  5. DarrenUnjuh表示:

    https://plavixclo.com/# buy clopidogrel online

  6. bokep viral表示:

    you’re truly a just right webmaster. The site loading
    speed is amazing. It sort of feels that you’re doing any unique trick.
    In addition, The contents are masterwork. you have performed a wonderful process
    in this matter!

  7. I liked this article. For additional info, visit mobile tire installation

  8. вывод из запоя на дому в краснодаре https://dmitrov.rusff.me/viewtopic.php?id=1657 .

  9. вывод из запоя стационар краснодар вывод из запоя стационар краснодар .

  10. BernardMop表示:

    ivermectin 4000 Ivermectin iver fast cost of ivermectin 1% cream

  11. вывод из запоя анонимно краснодар https://nsk.ukrbb.net/viewtopic.php?f=45&t=29670 .

  12. нарколог на дом вывод из запоя краснодар http://www.severussnape.borda.ru/?1-4-0-00000317-000-0-0-1730744751 .

  13. вывод из запоя стационар краснодар pelsh.forum24.ru/?1-8-0-00000126-000-0-0-1730745072 .

  14. вывод из запоя в стационаре краснодара http://zal.rolevaya.info/viewtopic.php?id=5360 .

  15. Faith Kanner表示:

    Land for own use or investment

  16. AlbertDuM表示:

    buy plavix: buy Plavix Clo – buy Clopidogrel over the counter

  17. A fascinating discussion is definitely worth comment. I believe that you need
    to publish more on this subject, it may not be a taboo
    matter but usually people do not talk about these topics.
    To the next! Many thanks!!

  18. вывод из запоя на дому в краснодаре вывод из запоя на дому в краснодаре .

  19. AlbertDuM表示:

    plavix medication: buy Plavix Clo – clopidogrel bisulfate 75 mg

  20. вывод из запоя кодирование краснодар belbeer.borda.ru/?1-6-0-00000757-000-0-0-1730745253 .

發佈留言

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