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,143,691 Responses

  1. pecah5000表示:

    This design is steller! You obviously know how to keep a reader entertained.

    Between your wit and your videos, I was almost moved to start my
    own blog (well, almost…HaHa!) Wonderful job. I really enjoyed
    what you had to say, and more than that, how you
    presented it. Too cool!

  2. gloves Cases表示:

    15 Unquestionable Reasons To Love CSGO Cases New gloves Cases

  3. ElmerWes表示:

    Buy Tadalafil 10mg Generic Tadalafil 20mg price Buy Tadalafil 10mg

  4. JamesNow表示:

    cheapest cenforce: cheapest cenforce – Purchase Cenforce Online

  5. 16 Must-Follow Instagram Pages For Private Mental Health Treatment-Related Businesses how do i get a mental health assessment

  6. ElmerWes表示:

    sildenafil 50 mg price Buy Viagra online cheap buy viagra here

  7. ElmerWes表示:

    Levitra online USA fast Levitra generic best price Levitra tablet price

  8. CharlesDog表示:

    https://cenforce.pro/# Buy Cenforce 100mg Online

  9. ElmerWes表示:

    Generic Tadalafil 20mg price Generic Tadalafil 20mg price Cialis over the counter

  10. wooden表示:

    How To Solve Issues With Double Glazed Windows Leeds wooden

  11. 11 Strategies To Completely Defy Your Cheap Online Shopping Sites
    Uk Garden Sharpener Card Pack

  12. glazier表示:

    5 Motives Windows And Doors Leeds Is Actually A Great Thing glazier

  13. JamesNow表示:

    Generic Cialis price: Generic Tadalafil 20mg price – cialis for sale

  14. Rosalind表示:

    Where Is Advanced Starter Kits Be One Year From This Year?
    free avon starter kit (Rosalind)

  15. Greate article. Keep writing such kind of information on your page.
    Im really impressed by it.
    Hi there, You have performed an excellent job.
    I will certainly digg it and individually recommend
    to my friends. I’m confident they will be benefited from
    this site.

  16. 20 Windows Repairs Websites Taking The Internet By Storm
    Window Repairs

  17. How To Recognize The Right Designer Handbags Sale For
    You Designer Handbags sale outlet

  18. ElmerWes表示:

    п»їBuy generic 100mg Viagra online Buy Viagra online cheap best price for viagra 100mg

  19. sightcare表示:

    Hey there! Do you know if they make any plugins to assist with SEO?
    I’m trying to get my blog to rank for some targeted keywords
    but I’m not seeing very good results. If you know of any please share.
    Thank you!

  20. JamesNow表示:

    sildenafil oral jelly 100mg kamagra: kamagra oral jelly – super kamagra

  21. 10 Inspiring Images About Double Glazing Repairs Near Me double glazing near me

  22. CharlesDog表示:

    https://kamagra.win/# buy kamagra online usa

  23. JamesNow表示:

    Cheap generic Viagra: viagra without prescription – Buy Viagra online cheap

  24. The Next Big Thing In Nissan Key Fob Replacement Nissan Key Replacement Prices

  25. What’s The Fuss About Automobile Locksmiths? automobile locksmiths near me

  26. JamesNow表示:

    Levitra price: Vardenafil online prescription – buy Levitra over the counter

  27. What’s up Dear, are you truly visiting this website on a regular basis,
    if so after that you will definitely get good know-how.

  28. asbestos表示:

    5 Killer Quora Answers To Asbestos Lawsuit asbestos

  29. volume pills表示:

    Having read this I thought it was extremely informative.
    I appreciate you spending some time and energy
    to put this information together. I once again find myself spending
    way too much time both reading and commenting. But so what, it was still worth
    it!

  30. 4 Dirty Little Secrets About The Kids Bunk Bed Industry sale bunk beds

發佈留言

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