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,667,366 Responses

  1. Vernon表示:

    %%

    My page – electrical certificate dunstable [Vernon]

  2. Excellent post. I was checking continuously this blog
    and I’m impressed! Extremely useful information specially the last part 🙂 I care for such information much.
    I was seeking this particular information for
    a long time. Thank you and best of luck.

  3. https://bogin10.z28.web.core.windows.net/research/bogin10-(4).html
    This should provide you with plenty of time to analysis, attempt on, order, and alter any dress you discover yourself purchasing.

  4. Anke Avent表示:

    https://storage.googleapis.com/bogin13/research/bogin13-(6).html
    This consists of most variations of white, corresponding to ivory and champagne.

  5. Eileen Nguyen表示:

    https://storage.googleapis.com/chinavisa11/research/chinavisa11-(115).html
    We even have tea-length dresses and lengthy attire to swimsuit any season, venue or preference.

  6. %%

    My web page 1500mg full spectrum cbd oil uk (find-Open.co.Uk)

  7. https://bogin3.s3.us-west-004.backblazeb2.com/research/bogin3-(24).html
    Jovani Plus size mother of the bride attire suits any body type.

  8. Hudson Salaam表示:

    https://bogin66.z32.web.core.windows.net/research/bogin66-(36).html
    Some ladies favor to wear a costume, whereas others prefer separates…and each are great options!

  9. https://bogin50.z1.web.core.windows.net/research/bogin50-(30).html
    “I stated right then I would wear it in the future,” she recalled.

  10. https://storage.googleapis.com/bogin25/research/bogin25-(33).html
    If you haven’t heard from her by about five months before the wedding, don’t be afraid to achieve out and ask for an update on the gown code.

  11. business表示:

    Very good blog! Do you have any suggestions for aspiring writers?
    I’m planning to start my own blog soon but I’m a little lost on everything.
    Would you suggest starting with a free platform like WordPress or go for a
    paid option? There are so many choices out there that I’m totally overwhelmed ..
    Any tips? Appreciate it!

  12. fajartoto 4d表示:

    Greetings, I believe your blog might be having
    web browser compatibility problems. Whenever I take a look
    at your web site in Safari, it looks fine but when opening in Internet Explorer, it’s got some overlapping issues.
    I merely wanted to give you a quick heads up!
    Other than that, wonderful website!

  13. Luisa表示:

    asbestos mesothelioma lawyer – Luisa

  14. Your blog post was a rather delightful read, I must admit!

    It’s not often you stumble upon something so captivating and insightful.
    Your writing style has a certain charming simplicity that makes it a delight to
    get lost in, and I truly appreciate that. The insights you’ve shared on this topic
    are thought-provoking. It’s evident that you
    have a comprehensive grasp of the subject matter, and I found your perspective to be quite refreshing.
    Thank you for taking the time to share your wisdom
    with your readers. I’m eagerly looking forward to your next post.

    Keep up the excellent work, it’s truly appreciated!

  15. https://filedn.eu/lXvDNJGJo3S0aUrNKUTnNkb/bogin11/research/bogin11-(169).html
    Cream is another choice or skirt and prime in a black and white mixture.

  16. Desmond表示:

    %%

    My blog – keys for mercedes (Desmond)

  17. chandal real madrid

  18. Ꭲhis is my first time pay а quick visit ɑt heree and
    i amm trᥙly hsppy tto rеad all аt οne placе.

    Here іs my webpage; Những mẫu đồng hồ nữ đẹp giá rẻ

  19. price表示:

    %%

    Here is my blog :: price

  20. Chi Tesch表示:

    https://seo38.z10.web.core.windows.net/research/seo38-(51).html
    With over five years of expertise in bridal, she is an professional on all issues fashion.

發佈回覆給「Eileen Nguyen」的留言 取消回覆

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