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,775 Responses

  1. pharmacy uk表示:

    Good day I am so happy I found your weblog, I really found you by accident,
    while I was browsing on Digg for something else, Regardless
    I am here now and would just like to say thanks for a tremendous post and a all round enjoyable
    blog (I also love the theme/design), I don’t have time to go through it all at the moment but I have book-marked it and
    also added in your RSS feeds, so when I have time I will be back to read a
    lot more, Please do keep up the excellent job.

  2. StevenNub表示:

    where buy clomid pills: clomid purchase online rex pharm – can you get generic clomid

  3. Pills prescribing information. What side effects?
    what is half life of buspirone
    Actual news about medicament. Get information now.

  4. Just want to say your article is as surprising. The clarity
    in your post is simply nice and i can assume you’re an expert on this
    subject. Well with your permission let me to grab your RSS feed to keep updated with forthcoming post.
    Thanks a million and please carry on the rewarding work.

  5. Nikefut表示:

    the dark internet blackweb official website https://darknetmarketstore.com/ – darkmarket list

  6. Drugs prescribing information. Long-Term Effects.
    levonorgestrel brand name
    Best what you want to know about medication. Read now.

  7. StevenNub表示:

    buy dapoxetine online: cheap priligy – priligy maxpharm

  8. BrandenSlomy表示:

    amoxicillin over counter: amoxil – cost of amoxicillin 30 capsules

  9. StevenNub表示:

    cost of cheap clomid without a prescription: clomid rex pharm – cheap clomid

  10. Curtisduh表示:

    BBgate MarketPlace 2024 Breaking Bad Gate Forum
    BBgate MarketPlace

  11. Dereketerm表示:

    where to buy cheap clomid tablets: buy clomid – order cheap clomid without prescription

  12. BrandenSlomy表示:

    where to get amoxicillin over the counter: Amoxicillin buy online – where can i get amoxicillin

  13. StevenNub表示:

    prednisone 10mg canada: generic Prednisone – prednisone without prescription medication

  14. This article will help the internet users for creating new webpage or even a
    weblog from start to end.

  15. Thanks a lot for sharing this with all people you really understand what you are talking approximately!
    Bookmarked. Kindly also discuss with my site =). We may have a link
    alternate agreement among us

  16. StevenNub表示:

    get clomid tablets: clomid online – where buy generic clomid online

  17. This is a great tip particularly to those fresh to the blogosphere.
    Simple but very precise info… Thanks for sharing this one.
    A must read article!

  18. StevenNub表示:

    where to get generic clomid price: clomid online – where buy generic clomid price

  19. There are various tools and websites that claim to allow users
    to how do you view private instagram accounts private Instagram profiles, but it’s important to retrieve these later than caution. Many
    of these tools can be unreliable, may require personal information, or could violate
    Instagram’s terms of service. Additionally, using such tools can compromise your own security or lead to scams.
    The safest and most ethical exaggeration to view
    a private profile is to send a follow request directly to the user.
    Always prioritize privacy and exaltation in your online interactions.

發佈留言

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