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,626,056 Responses

  1. 5 Killer Quora Answers To Compact Treadmill With Incline Compact Treadmill With Incline

  2. That is actually engaging, You’re an overly adept blogger. I’ve coupled your rss feed and sit up for in quest of more of your magnificent post. Additionally, I’ve shared your web site in my social networks!

    my web blog – http://Forums.outdoorreview.com/member.php?293289-Sergprl

  3. Meds information for patients. What side effects can this medication cause?
    otc alternative to pantoprazole
    Actual trends of medicament. Read here.

  4. LeroyBak表示:

    vitamins for ed low cost prescription ed online pharmacy

  5. How To Tell The Window Glass Replacement Near Me Which Is Right For
    You replacement Glass panels

  6. shenasname.ir表示:

    A Productive Rant Concerning Electric Wall Mounted Fire
    electric fireplace hang On wall – shenasname.ir,

  7. LeroyBak表示:

    erectile dysfunction natural remedies low cost prescription natural remedies for ed problems

  8. Coreycob表示:

    Medication reactions explained. Medicine facts available.
    how to order prednisone online
    Medication leaflet available. Latest drug developments.

  9. Thanks a bunch for sharing this with all people you really realize
    what you’re talking approximately! Bookmarked. Kindly
    additionally visit my web site =). We will have a link exchange contract
    between us

  10. DevonPhack表示:

    http://mexicanpharm24.cheap/# mexico drug stores pharmacies
    best ed pill

  11. Greetings from Ohio! I’m bored to tears at work
    so I decided to browse your website on my iphone during lunch break.

    I love the info you provide here and can’t wait to take a look
    when I get home. I’m amazed at how quick your blog loaded on my mobile ..

    I’m not even using WIFI, just 3G .. Anyways, fantastic blog!

  12. BrandonSaisy表示:

    mexican pharmaceuticals online: mexico pharmacy – best online pharmacies in mexico

  13. Christine表示:

    Hi everyone, it’s my first pay a visit at this site, and article is really fruitful in favor of me, keep up posting these content.

  14. DevonPhack表示:

    http://indianpharm24.pro/# indian pharmacy paypal
    ed medications comparison

  15. 10 Things You Learned In Kindergarden They’ll Help You Understand Get Diagnosed With ADHD how To get diagnosed with add as an adult

  16. It’s difficult to find well-informed people in this particular topic, but you sound like you know what you’re talking about!
    Thanks

  17. Williamagric表示:

    natural help for ed https://indianpharm24.pro/# reputable indian pharmacies

  18. Williamagric表示:

    anti fungal pills without prescription https://indianpharm24.pro/# indianpharmacy com

  19. Keep on writing, great job!

  20. The Most Pervasive Issues With Mobility Scooter For Travel User-Friendly
    Scooters (Rojas-Borch-7.Technetbloggers.De)

  21. You’ll Never Guess This Best Coffee Machine Pod’s Tricks best cappuccino machine

  22. Williamagric表示:

    male dysfunction treatment http://mexicanpharm24.cheap/# mexican online pharmacies prescription drugs

  23. Jorgeagopy表示:

    prices of viagra at walmart: cheap pharmacy online – natural pills for ed

  24. It�s hard to find well-informed folks on this subject, however, you sound like you know what you�re talking about! Thanks

    My web-site – http://Forum.D-Dub.com/member.php?501806-Serggni

  25. 20 Trailblazers Are Leading The Way In Car Lock Smith car locksmith Near me cheap

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

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