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,368,013 Responses

  1. heib.ru表示:

    A Provocative Remark About Buy A Driving License With
    Code 95 Kup prawo jazdy Z kodem 95 (heib.ru)

  2. RobertJar表示:

    buy cipro no rx: ci pharm delivery – ciprofloxacin 500 mg tablet price

  3. KennethPah表示:

    cost of cheap clomid pill can i buy generic clomid pills can i purchase generic clomid online

  4. Awesome 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 jump out.
    Please let me know where you got your design. Many thanks

  5. Hello to all, the contents existing at this site are in fact awesome for
    people knowledge, well, keep up the nice work fellows.

  6. SamuelBix表示:

    cost generic clomid now: clomid on pharm – where to get cheap clomid price

  7. xnxx.com表示:

    I like it when individuals get together and share opinions.
    Great blog, keep it up!

  8. KennethPah表示:

    prednisone over the counter south africa PredniBest prednisone 5 mg brand name

  9. SamuelBix表示:

    cheap clomid: clomidonpharm – buying cheap clomid without dr prescription

  10. EdgarWat表示:

    Интересные статьи о займах – http://worldcrisis.ru/crisis/3973650

  11. I need to to thank you for this good read!! I definitely loved every little bit of it.
    I have got you bookmarked to look at new stuff you post…

  12. Nikefut表示:

    drug markets onion darknet market darknet market lists

  13. This post has opened my eyes to the variety of options available for bathroom renovations! Explore more at Bathroom refurbishment

  14. DanielAbice表示:

    https://prednibest.com/# prednisone pills cost

  15. SamuelBix表示:

    ordering prednisone: best pharmacy prednisone – prednisone 2 mg daily

  16. Bet777 Casino表示:

    I always used to read article in news papers but now as I am a user of internet so from now I am
    using net for articles, thanks to web.

  17. SamuelBix表示:

    prednisone cost 10mg: PredniBest – buy prednisone online without a prescription

  18. Building a company on-line requires advantage, and an outstanding electronic marketing employer could make the entire difference Digital Marketing Agency

  19. DanielAbice表示:

    https://prednibest.com/# generic prednisone cost

  20. Nikefut表示:

    dark net dark internet dark web sites

  21. Its like you read my mind! You appear to know a lot about this, like you wrote the book in it or
    something. I think that you could do with some pics to drive the
    message home a little bit, but instead of that, this is fantastic blog.
    A fantastic read. I’ll definitely be back.

  22. Soft washing actually helps maintain the visual appeals of your property without damage– fantastic topic covered here; discover more at http://riverpnxd748.cavandoragh.org/what-is-soft-washing-and-how-does-it-differ-from-pressure-washing

  1. 2024-12-23

    tadalafil review forum

    tadalafil review forum

發佈留言

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