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,230,463 Responses

  1. Ronaldpsymn表示:

    http://cytotec.club/# cytotec pills buy online

  2. The 10 Most Scariest Things About Online Shopping Stores List microsoft rf keyboard and mouse

  3. Guide To Cost Of Private ADHD Assessment UK:
    The Intermediate Guide For Cost Of Private ADHD Assessment UK
    cost of private adhd assessment uk

  4. This Is The Ultimate Guide To Auto Folding Mobility Scooters Uk folding electric Scooter for adults

  5. The 3 Biggest Disasters In Designer Handbags Brown The Designer Handbags
    Brown’s 3 Biggest Disasters In History Designer Handbags Women’s (http://Www.Stes.Tyc.Edu.Tw)

  6. Ronaldpsymn表示:

    https://lisinopril.network/# buy lisinopril 2.5 mg online

  7. 7 Simple Secrets To Totally You Into Locksmiths Car cheap car locksmiths Near me

  8. RandallTaulp表示:

    cost cheap propecia no prescription: cost of cheap propecia without rx – buy generic propecia no prescription

  9. 5 Automobile Locksmith Tips You Must Know About For
    2023 cheap auto locksmith (https://schultz-pappas.technetbloggers.de/)

  10. See What Adhd Medication List Tricks The Celebs Are Utilizing Adhd Medication List

  11. 10 Citroen Key Replacement That Are Unexpected citroen C1 key replacement price (http://Www.cheaperseeker.com)

  12. RandallTaulp表示:

    order propecia: cost propecia online – cost propecia online

  13. Tntech.kr表示:

    Double Glazing Repairs Near Me Tools To Help You Manage Your Daily Lifethe One Double Glazing Repairs Near Me Technique Every
    Person Needs To Learn Double glazing repairs Near me (Tntech.kr)

  14. What Is Everyone Talking About Glass Repair Leeds Right Now
    window hinges leeds

  15. Best Vibrator For Clit Tools To Ease Your Everyday Lifethe Only Best Vibrator For Clit Trick
    That Everyone Should Know best vibrator for clit

  16. Davidtes表示:

    online pharmacy lisinopril lisinopril prescription cost lisinopril 20 mg tabs

  17. RandallTaulp表示:

    where to buy nolvadex: tamoxifen and grapefruit – nolvadex only pct

  18. Why We Are In Love With Espresso Machine Sale (And You Should Also!) home espresso machine

  19. 15 Things You’ve Never Known About Window Repair Near window Repairs

  20. RandallTaulp表示:

    buy misoprostol over the counter: cytotec abortion pill – buy cytotec in usa

  21. Davidtes表示:

    buy cytotec online cytotec pills buy online buy cytotec

  22. What Is The Future Of Double Glazing Window Repairs Near Me Be Like In 100 Years?
    replacement double glazed glass only (http://bitetheass.com/user/sprucemaple8/)

  23. Here’s A Little-Known Fact Regarding Adult Female Adhd Symptoms
    common symptoms of Adhd In females

  24. Ronaldpsymn表示:

    https://lisinopril.network/# lisinopril 20mg coupon

  25. This Is The Desk Treadmill Case Study You’ll Never Forget small under Desk treadmill

  26. Davidtes表示:

    where can i buy cipro online ciprofloxacin 500 mg tablet price where can i buy cipro online

  27. replacement表示:

    20 Trailblazers Lead The Way In Double Glazing In Leeds replacement

  28. Davidtes表示:

    propecia buy cost of generic propecia cost generic propecia

發佈留言

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