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,585 Responses

  1. What’s up to every one, the contents present
    at this website are genuinely amazing for people
    experience, well, keep up the good work fellows.

  2. Hey There. I discovered your blog the use of
    msn. That is an extremely neatly written article.
    I’ll be sure to bookmark it and come back to read
    extra of your helpful info. Thank you for the post.

    I’ll certainly comeback.

  3. StevenNub表示:

    prednisone 40 mg daily: generic Prednisone – prednisone capsules

  4. Lazrypx表示:

    Купить диплом о среднем полном образовании, в чем подвох и как избежать обмана?
    amegaklimat.ru/forum/?PAGE_NAME=profile_view&UID=3590

  5. Dereketerm表示:

    canadian pharmacy amoxicillin: Amoxicillin buy online – amoxicillin 500mg price canada

  6. BrandenSlomy表示:

    max pharm: buy priligy – priligy maxpharm

  7. Danieljek表示:

    amoxil pharmacy https://priligymaxpharm.com/# buy dapoxetine online

  8. StevenNub表示:

    buy amoxicillin 500mg uk: Amoxicillin Com Pharm – amoxicillin 500mg without prescription

  9. Casino表示:

    I’m gone to inform my little brother, that he should also pay
    a visit this webpage on regular basis to get updated from
    newest reports.

  10. Köp Av C-körkort: Steg För Steg Guide köpa körkortonline [https://anotepad.com/notes/e8fb7eas]

  11. JacobLoody表示:

    кухни москва – Широкий выбор кухонь для установки в квартирах и домах Москвы.

  12. Dereketerm表示:

    can you get clomid no prescription: buy clomid – can you buy cheap clomid without dr prescription

  13. Hello there, I believe your site could be having web browser compatibility
    issues. Whenever I take a look at your blog in Safari, it looks fine however,
    when opening in Internet Explorer, it has some overlapping issues.
    I just wanted to give you a quick heads up! Besides that, great site!

  14. StevenNub表示:

    max pharm: max pharm – priligy

  15. ラブドール えろleast fun was the Remoji M-Cup.I don’t love it.

  16. I like what you guys are usually up too. This kind of clever work and coverage!
    Keep up the awesome works guys I’ve included you guys to my blogroll.

  17. Dereketerm表示:

    pharmacy cost of prednisone: ray pharm – buy prednisone canadian pharmacy

  18. Aproveite as mais abrangentes interpretacoes.
    Sonha com um cao que morde o seu braco? Doeu e sangrou? Interpretacao de um sonho de acordo com diferentes livros de sonho

    [url=http://alasdelsur.com/2022/07/01/pack-wisely-before-traveling/#comment-163819]Olhar num espelho num sonho[/url] [url=https://blog.momitsubo.jp/tyougoukin/comment-page-1434/#comment-3803624]Porque e que sonho com uma cruz partida?[/url] [url=http://personaltrainerberlin.de/imperssum/#comment-206577]Com que e que um elefante sonha?[/url] [url=http://joomla.ccnsteam.com/index.php?option=com_fireboard&Itemid=4&func=view&catid=3&id=6978#6978]O que e que eu sonho em mudar de emprego?[/url] [url=https://hpmcor.com/dogs_cats_hotel/2023/01/06/dogs/#comment-52523]O que e que eu sonho em mudar de emprego?[/url] d52_a3e

  19. StevenNub表示:

    amoxicillin without a doctors prescription: Amoxicillin for sale – amoxicillin price without insurance

  20. BrandenSlomy表示:

    buy priligy: Priligy tablets – cheap priligy

  21. Dereketerm表示:

    can i order clomid without prescription: rexpharm – where can i buy clomid prices

  22. StevenNub表示:

    how can i get cheap clomid for sale: rex pharm – order clomid without a prescription

發佈留言

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