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,247,283 Responses

  1. I am curious to find out what blog platform you have been working with?
    I’m having some small security problems with my latest website and
    I would like to find something more safe. Do you have any solutions?

  2. Nikefut表示:

    deep web drug markets bitcoin dark web best darknet markets

  3. DarrenUnjuh表示:

    https://iverfast.com/# order minocycline 100mg online

  4. BrandenSlomy表示:

    buy priligy: max pharm – cheap priligy

  5. Hello to every one, the contents present at this web page are truly awesome for people experience, well, keep up the nice work
    fellows.

  6. nodeposit表示:

    Excellent article! We will be linking to this great content on our site.
    Keep up the good writing.

  7. An compelling discussion is worth commentary. There’s no doubt that that you should write read more about this topic, it might not be a taboo subject but generally people don’t discuss so. To the next! Kind regards!!

    my web site :: http://Oople.com/forums/member.php?u=246522

  8. Mariosep表示:

    Пошаговое руководство по восстановлению прозрачности фар с помощью наждачной бумаги разной зернистости. Советы по выбору материалов и технике обработки https://aggam.xyz/service/5-glavnyh-oshibok-pri-vybore-nazhdachnoj-bumagi-dlya-polirovki-avtomobilnyh-far/

  9. BrandenSlomy表示:

    generic amoxicillin over the counter: cheap amoxil – amoxicillin 500 mg tablets

  10. выведение из запоя воронеж стационар https://kvitka.ukrbb.net/viewtopic.php?f=58&t=28033 .

  11. DarrenUnjuh表示:

    http://iverfast.com/# ivermectin oral 0 8

  12. Higly Demand of Durian Land

  13. BrandenSlomy表示:

    cheap priligy: priligy maxpharm – buy priligy

  14. Katja表示:

    Guide To Metal L Shaped Bunk Beds: The Intermediate Guide On Metal L Shaped Bunk Beds metal l shaped bunk beds (Katja)

  15. xxx表示:

    I have read several good stuff here. Definitely value bookmarking for revisiting.

    I wonder how so much attempt you put to create such a magnificent informative site.

  16. 5 Reasons To Be An Online Fleshlight For Men Business And
    5 Reasons To Not Best Of Fleshlight

  17. Land price always will increase because of demands

  18. An compelling conversation is worth commentary. I believe is that you need to write find out more on this particular topic, it may not be a taboo topic but generally people don’t discuss so. To the next! Best wishes!!

    Check out my webpage :: http://www.Adtgamer.com.br/showthread.php?p=498395

  19. Hi, its pleasant article concerning media print, we all know media is a great source of facts.

  20. Good tips Many thanks!

  21. xxx表示:

    Everything is very open with a clear description of the challenges.
    It was really informative. Your website is extremely helpful.
    Thank you for sharing!

  22. I’m not that much of a internet reader to be
    honest but your sites really nice, keep it up! I’ll go ahead and
    bookmark your website to come back down the
    road. All the best

  23. AlbertDuM表示:

    cheap plavix antiplatelet drug: buy plavix online – buy plavix

發佈留言

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