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,367,298 Responses

  1. RobertJar表示:

    buying amoxicillin online: Amox Star – order amoxicillin no prescription

  2. Процесс верификации в Пин ап казино может
    длится от пары-тройки часов до нескольких
    дней зависимо от правильности предоставленных.

    Here is my webpage бк мостбет

  3. KennethPah表示:

    prednisone cream rx PredniBest prednisone capsules

  4. Nikefut表示:

    darknet search engine tor markets 2024 darknet sites

  5. There’s Enough! 15 Things About Capsule Machine Coffee We’re Sick Of Hearing capsule coffee maker

  6. This piece of writing will help the internet visitors for setting up new blog
    or even a blog from start to end.

  7. News Today表示:

    Generally I do not read post on blogs, however I would like to say that this write-up very compelled me to take a look at and do so!

    Your writing taste has been surprised me.
    Thanks, very nice post.

  8. KennethPah表示:

    prednisone 50 mg tablet cost prednisone without prescription where to get prednisone

  9. You need to take part in a contest for one of the best blogs on the web.
    I most certainly will recommend this blog!

  10. Carmina Berth表示:

    Woah! I’m really digging the template/theme of this blog. It’s simple, yet effective. A lot of times it’s difficult to get that “perfect balance” between user friendliness and appearance. I must say that you’ve done a superb job with this. Additionally, the blog loads extremely fast for me on Internet explorer. Superb Blog!

  11. MatthewHob表示:

    An article worth your time—thought you’d like it http://koex21.co.kr/bbs/board.php?bo_table=free&wr_id=215730

  12. After looking at a number of the blog articles on your blog, I really like your
    technique of blogging. I book-marked it to
    my bookmark webpage list and will be checking back in the near future.
    Take a look at my website as well and let me know your opinion. https://nlifelab.org/bbs/board.php?bo_table=free&wr_id=963882

  13. Victor Lloyd表示:

    Your insights are incredibly helpful—working with an effective # online marketing for lawyers # could transform our client

  14. JohnnyHasse表示:

    When choosing a new personalized birthday present for your spouse, think beyond typical presents and give attention to creating meaningful experience. A custom piece, engraved with the special date or even message, combines beauty with sentiment, while a tailored piece of art that will reflects a ram gives a deeply individual touch.

    For the even more interactive gift, look at a curated scrapbooking or photo recording that highlights milestones inside your relationship. On the other hand, custom-made home personal, for example a personalized baby blanket or family brand sign, offers the two practicality and emotional value.

    https://techbullion.com/9-meaningful-personalized-birthday-gifts-for-your-spouse/

    If your current spouse enjoys a great unique experience, look at planning a delight getaway or the personalized adventure time. These thoughtful, custom-made gifts strengthen your connection and create sustained memories, showing your partner how well an individual truly know and even appreciate them.

  15. The 10 Scariest Things About Best Delonghi Coffee Machine best Delonghi coffee Machine

  16. Greetings from Ohio! I’m bored at work so I decided to check out your blog on my iphone during lunch break.
    I love the knowledge you provide here and can’t wait to take a look when I get home.
    I’m surprised at how fast your blog loaded on my mobile ..
    I’m not even using WIFI, just 3G .. Anyways, superb site!

    Feel free to visit my web blog – specialized Alzheimer’s care

  17. SamuelBix表示:

    buy cipro without rx: ci pharm delivery – cipro pharmacy

  18. Emelia表示:

    Unexpected Business Strategies For Business That Aided
    Drip Coffee Pot Succeed drip coffee equipment [Emelia]

  19. Nancy Quinn表示:

    What an enlightening read about using chiropractic care as part of recovery from car accidents; very encouraging for victims like me! Chiropractic treatment for whiplash and neck pain

發佈留言

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