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,477,016 Responses

  1. Urielmew表示:

    Послушайте.
    due to the effect of lower prices it is important that https://bitcoin-recovery.io/ always very expensive for beginners.

  2. ta88vnninfo表示:

    I am really impressed together with your writing abilities
    and also with the structure on your blog. Is that this a paid topic or did you customize it yourself?
    Anyway stay up the excellent high quality writing, it’s uncommon to look a nice
    weblog like this one today..

  3. сколько стоит сделать загранпаспорт сколько стоит сделать загранпаспорт .

  4. срочное оформление загранпаспорта срочное оформление загранпаспорта .

  5. It’s remarkable designed for me to have a web page, which is useful in support of my
    experience. thanks admin

  6. срочное оформление загранпаспорта срочное оформление загранпаспорта .

  7. Appreciate it. Plenty of knowledge.

  8. Mighty Dog Roofing
    Reimer Driive North 13768
    Maplle Grove, MN 55311 United Տtates
    (763) 280-5115
    affordable storm restorations

  9. Charlesbot表示:

    https://maxpillsformen.com/# Cialis without a doctor prescription

  10. Donaldabula表示:

    Viagra generic over the counter viagra canada Cheap Viagra 100mg

  11. Thanks for the comprehensive read. Find more at outdoor lighting near me

  12. It’s going tߋ be finish of mine day, however before end
    I am reading this great pіece of writing to improve my knowledge.

    Feel free to visit my page – matic car rental bandung

  13. How To Live The Annual Office Party – 10 Suggestions 출장안마 (j-chin.music.coocan.jp)

  14. срочное оформление загранпаспорта срочное оформление загранпаспорта .

  15. BrettTIT表示:

    Buy generic 100mg Viagra online: FastPillsForMen – sildenafil online

  16. Hey There. I found your blog using msn. This is a really well written article.
    I will be sure to bookmark it and return to
    read more of your useful info. Thanks for the post. I will certainly
    comeback.

  17. 하이오피表示:

    The Ultimate Guide For The Ultimate Nightlife Destinations 하이오피

  18. 10 Misconceptions Your Boss Has About Search Engine Optimization Agency Search engine optimisation manchester

  19. Donaldabula表示:

    Generic Cialis without a doctor prescription Cialis over the counter п»їcialis generic

  20. Donaldabula表示:

    cheapest online ed treatment cheap cialis cheapest ed medication

  21. RogerFaf表示:

    https://fastpillseasy.com/# online ed pharmacy

  22. 유흥表示:

    5 1 Tip For Marketing Your Online Business 유흥

  23. Hello mates, how is all, and what you wish for to say on the topic of this
    paragraph, in my view its genuinely amazing in support
    of me.

  24. Blanca表示:

    The Companies That Are The Least Well-Known To In The Car Accident Attorneys Near Me Industry
    lawyer for car accidents near me (Blanca)

  25. Jose Sandoval表示:

    Thanks for the thorough article. Find more at access platforms

  26. Charlesbot表示:

    http://fastpillsformen.com/# Sildenafil Citrate Tablets 100mg

發佈留言

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