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

  1. The 10 Most Scariest Things About Corner Wood Burning Stove corner wood Burning stove

  2. Random 77表示:

    Hi Dear, are you in fact visiting this website daily, if so then you
    will without doubt get good knowledge.

  3. You should take part in a contest for one of the highest quality blogs on the
    web. I am going to highly recommend this website!

  4. LarryCoell表示:

    en cok kazand?ran slot oyunlar? slot tr online slot tr online

  5. LarryCoell表示:

    matadorbet bid matadorbet.bid matadorbet bid

  6. CarrollUnton表示:

    deneme bonusu veren siteler yerliarama.org http://ultrabet-tr.online/# ultrabet giris

  7. 10 Pragmatic Return Rate Tips All Experts Recommend 프라그마틱 슬롯무료

  8. Seo Harvard表示:

    Hey there exceptional blog! Does running a blog similar to this take a
    massive amount work? I have absolutely no
    expertise in programming but I had been hoping to start my own blog
    in the near future. Anyways, should you have any ideas or techniques for new blog owners please share.
    I know this is off topic however I just wanted
    to ask. Thank you!

  9. Alexandria表示:

    It’s Time To Increase Your Pram Pushchair 2 In 1 Options car seat 2 in 1
    stroller (Alexandria)

  10. Some Of The Most Common Mistakes People Make With Key Lamborghini Aventador lamborghini Key

  11. Uazryax表示:

    Быстрая схема покупки диплома старого образца: что важно знать?

    coolgopro.dk/favicon.ico

  12. Where Can You Get The Most Effective Window Glass Repair Information?
    Misted Glass Repair

  13. See What Auto Locksmith Service Tricks The Celebs Are Making Use Of auto Locksmith Service

  14. My coder is trying to persuade me to move to .net from PHP.
    I have always disliked the idea because of the expenses.

    But he’s tryiong none the less. I’ve been using WordPress
    on numerous websites for about a year and am anxious about switching to another platform.
    I have heard great things about blogengine.net. Is there a way I can transfer all
    my wordpress content into it? Any help would be really appreciated!

  15. Meds prescribing information. Effects of Drug Abuse.
    naltrexone inflammatory bowel disease
    Best about medicines. Get information here.

  16. chute closer表示:

    Excellent post. I was checking constantly this weblog and I am inspired!
    Very helpful information specifically the ultimate phase 🙂 I deal with such info a lot.
    I used to be looking for this certain info for a long time.
    Thank you and best of luck.

  17. CarrollUnton表示:

    deneme bonusu veren siteler yerliarama.org https://denemebonusuverensiteler.top/# deneme bonusu veren siteler yerliarama.org

  18. 14 Smart Ways To Spend The Remaining Car Key Cover Fiat 500
    Budget fiat punto replacement Key

  19. 9 Lessons Your Parents Taught You About Tilt And Turn Windows Aluminium tilt and turn windows aluminium

  20. Uazrefi表示:

    Официальная покупка диплома вуза с сокращенной программой в Москве

    benhvienthammyasean.com/kupit-diplom-agrarnogo-menedzhera.html

  21. Derekfar表示:

    en cok kazand?ran slot oyunlar?: slot oyunlar? puf noktalar? – az parayla cok kazandiran slot oyunlar?

  22. Don’t Buy Into These “Trends” Concerning Small Single Bunk Small Single Bunk Beds

  23. Derekfar表示:

    Canl? Casino Siteleri: Casino Siteleri – Casino Siteleri

  24. LarryCoell表示:

    ultrabet ultrabet giris ultrabet yeni giris 1125

  25. What Is Situs Toto? To Use It toto macau (Ezproxy.cityu.edu.hk)

發佈留言

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