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

  1. LsfPaish表示:

    Consumers are aware of low price of side effects of flagyl from the Internet.

  2. Hi, its nice piece of writing concerning media print, we all be aware of media is a
    great source of data.

  3. Diplomi_phEa表示:

    где заказать дипломную работу 1oriks-diplom199.ru .

  4. Diplomi_tzEa表示:

    купить диплом инженера электрика 1oriks-diplom199.ru .

  5. Bradleygoath表示:

    online prednisone 5mg generic Prednisone prednisone 2.5 mg daily

  6. Узнай все о лапароскопия варикоцеле варикоцеле у мужчин

  7. StevenNub表示:

    priligy maxpharm: dapoxetine online – priligy

  8. вызов нарколога на дом краснодар planeta.mybb.social/viewtopic.php?id=2227 .

  9. Nikefut表示:

    darkmarket url darknet search engine tor markets links

  10. xxx表示:

    My partner and I absolutely love your blog and find many of your post’s to be what
    precisely I’m looking for. Does one offer guest writers to write content for yourself?

    I wouldn’t mind producing a post or elaborating on a lot of
    the subjects you write related to here. Again, awesome web log!

  11. Dereketerm表示:

    order cheap clomid without rx: rexpharm – can i purchase generic clomid without dr prescription

  12. RajaBandot表示:

    Ԍreetings! Ꮩery usefսl advice ԝithin thiѕ article!
    Ӏt’s tһe little changes that maке tthe ost impоrtant changes.
    Ꮇany tһanks foг sharing!

    Here is mу web-site; RajaBandot

  13. BrandenSlomy表示:

    cheap priligy: buy dapoxetine online – dapoxetine price

  14. After looking into a number of the blog articles on your
    blog, I truly appreciate your way of blogging. I bookmarked it to my
    bookmark website list and will be checking back soon.
    Please visit my website as well and tell me your opinion.

  15. When someone writes an article he/she retains the thought of a user
    in his/her brain that how a user can understand it.
    So that’s why this piece of writing is amazing. Thanks!

  16. Тут можно преобрести оружейные сейфы москва оружейный сейф в москве

  17. BrandenSlomy表示:

    where to buy generic clomid tablets: clomid – get generic clomid tablets

  18. I for all time emailed this blog post page to all my associates, as if like to read it after that my links will too.

  19. Узнай все о варикоцеле левого яичка признаки варикоцеле

  20. What an illuminating and reflective article !

    I ought to declare , your scrutiny of this vital subject was truly exceptional .

    The breadth and nuance you infused to the discourse was exceptional, projecting new illumination on the subtleties at
    work .
    I found myself agreeing as I scanned through your proficiently composed
    assertions . The manner you were equipped to distill
    the core ideas minus oversimplifying was particularly
    impressive .
    It’s evident you’ve committed a substantial amount of energy
    into studying this matter .
    This write-up has provided me plenty to contemplate and has prompted me to reassess particular components of
    my personal mindset.
    I appreciate you investing the resources to convey
    your proficiency – write-ups like this are immensely
    valuable in developing the wider dialogue .
    I await with excitement scanning more of your content in the months to
    come . Kindly keep up the excellent endeavors !

    My page: microgaming betway (microgaming.free.site.pro)

發佈留言

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