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,238,227 Responses

  1. Why The Biggest “Myths” About Samsung Freezers Could Actually Be Accurate samsung Refrigerator models

  2. 5 Killer Quora Answers To ADHD Private Assessment UK Adhd private Assessment Uk

  3. Derekfar表示:

    guvenilir casino siteleri: Casino Siteleri – casino siteleri win

  4. porn sex表示:

    I?m impressed, I must say. Seldom do I encounter a blog that?s both equally educative and interesting, and without a doubt, you have hit
    the nail on the head. The problem is something which
    not enough folks are speaking intelligently about.
    Now i’m very happy that I stumbled across this during my search
    for something regarding this.

  5. ClydePab表示:

    Casino Siteleri: Casino Siteleri – Canl? Casino Siteleri

  6. The 9 Things Your Parents Taught You About Single
    Stroller With Bench Seat Single Stroller With Bench Seat

  7. Uazroxz表示:

    Покупка диплома о среднем полном образовании: как избежать мошенничества?
    mir-kolyasok.ru/kupit-attestat-v-melitopole.html

  8. ClydePab表示:

    deneme bonusu veren siteler mycbet.com: deneme bonusu veren siteler denemebonusu2026.com – denemebonusuverensiteler.top

  9. Gus表示:

    10 Top Books On Case Battles case battle cs go (Gus)

  10. Greetings! Very useful advice in this particular post!
    It is the little changes which will make the largest changes.
    Thanks for sharing!

  11. Uazrrfi表示:

    Как купить аттестат 11 класса с официальным упрощенным обучением в Москве

    go4pro.ru/kupit-attestat-za-9-klass-2023.html

  12. Uazrpns表示:

    Всё, что нужно знать о покупке аттестата о среднем образовании
    bania-bochka.ru/gde-kupit-diplom-kolledzha-v-odesse.html

  13. Hi there everybody, here every person is sharing these kinds of familiarity, so it’s nice to read this weblog, and I used to pay
    a visit this blog daily.

  14. Window Glass Repairs It’s Not As Expensive As You
    Think Repairing Glass windows

  15. LarryCoell表示:

    az parayla cok kazandiran slot oyunlar? az parayla cok kazandiran slot oyunlar? en kazancl? slot oyunlar?

  16. Very good article. I certainly love this site. Keep writing!

  17. view and download instagram stories https://storyinst.com/ .

  18. Если хотите начать с преимуществом, активируйте промокод на лаки джет.

  19. Why Car Accident Lawyers Near Me Is Everywhere This Year car
    crash attorney – https://king-Wifi.win/Wiki/glennbergmann5362,

  20. Thanks for one’s marvelous posting! I definitely enjoyed reading it, you
    could be a great author.I will ensure that I bookmark your blog and will often come back from now on. I want
    to encourage continue your great job, have a nice morning!

  21. Peculiar article, just what I wanted to find.

發佈留言

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