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,226,346 Responses

  1. You actually reported that really well!

    Here is my page: https://staceyembracingchange.com/

  2. Double Bunk Tools To Ease Your Daily Life Double Bunk Technique Every Person Needs To
    Know double bed Double bunk (https://ocelotlace0.bravejournal.net)

  3. Davidtes表示:

    tamoxifen hip pain does tamoxifen cause joint pain tamoxifen for sale

  4. 9 . What Your Parents Taught You About Upvc Window Repairs
    Near Me Upvc window repairs near me

  5. Five Killer Quora Answers To Fridge Freezer For Sale fridge freezer for Sale

  6. Ronaldpsymn表示:

    http://finasteride.store/# buying propecia without prescription

  7. Five People You Must Know In The Small Double Mattresses Industry double mattress sale uk

  8. Davidtes表示:

    cipro ciprofloxacin where can i buy cipro online buy cipro online without prescription

  9. RandallTaulp表示:

    lisinopril 4 mg: lisinopril 25 mg cost – buy lisinopril uk

  10. Eusebia表示:

    Ghost Installations Tips That Will Change Your Life
    ghost installations, Eusebia,

  11. Bunk Bed Online Store Tools To Ease Your Daily Lifethe One Bunk Bed Online
    Store Trick That Every Person Should Be Able To bunk Bed online

  12. Adolph表示:

    What The 10 Most Worst Bean Coffee Maker FAILURES Of All Time Could Have Been Prevented bean to cup machines (Adolph)

  13. Guide To Online Shopping Sites In United Kingdom: The Intermediate Guide In Online
    Shopping Sites In United Kingdom online shopping sites In united kingdom

  14. Davidtes表示:

    ciprofloxacin 500 mg tablet price cipro online no prescription in the usa buy cipro online

  15. quietmona.Com表示:

    14 Businesses Doing An Amazing Job At CSGO Cases Highest
    Roi cs20 case – quietmona.Com,

  16. See What Retro Espresso Machine Tricks The Celebs Are Making Use Of retro espresso Machine

  17. Hey there I am so delighted I found your website, I really found
    you by mistake, while I was looking on Bing
    for something else, Nonetheless I am here now and would just
    like to say thanks for a fantastic post and a all round enjoyable blog (I also
    love the theme/design), I don’t have time to browse it all at
    the minute but I have book-marked it and also added in your RSS feeds, so when I have time I will be back to
    read more, Please do keep up the fantastic work.

  18. Interesting blog! Is your theme custom made or did you download it
    from somewhere? A theme like yours with a few simple tweeks
    would really make my blog stand out. Please let me know where you got your theme.
    Bless you

    Here is my web page; 실시간중계

  19. Near By表示:

    Why You Should Concentrate On Making Improvements To Repairing Upvc Windows Near By

  20. titration表示:

    Titration ADHD Meds Tools To Ease Your Everyday Lifethe Only Titration ADHD Meds Trick
    That Should Be Used By Everyone Know titration

  21. Five Things You Didn’t Know About Ford Replacement Key Programming replacement keys for Ford (http://lineyka.org/)

  22. RandallTaulp表示:

    lisinopril brand name australia: buy lisinopril 20 mg without prescription – lisinopril 10 mg online no prescription

  23. 3 Common Causes For Why Your Keys For Mercedes Isn’t Working (And Solutions
    To Resolve It) Mercedes Replacement Keys

  24. 5 Killer Quora Answers To Psychiatry Assessment Uk Psychiatry Assessment Uk

  25. RandallTaulp表示:

    antibiotics cipro: ciprofloxacin mail online – cipro 500mg best prices

  26. b.cari.com.my表示:

    15 Mental Health Counselor Near Me Benefits That Everyone
    Should Know mental health assessments near me (b.cari.com.my)

  27. The Most Inspirational Sources Of Mobile Auto Diagnostic Services
    Near Me Engine Diagnostics

發佈留言

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