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

  1. See What Seo Backlink Building Software Tricks The Celebs Are Making
    Use Of seo Backlink building software

  2. LeroyBak表示:

    legal to buy prescription drugs without prescription low cost pharmacy canada ed drugs

  3. jili slot表示:

    My brother suggested I would possibly like this web site.
    He was totally right. This publish actually made my day. You cann’t consider simply how so much time I
    had spent for this information! Thanks! jili slot

  4. 10 Erroneous Answers To Common Cheap Sofas For Sale Questions: Do
    You Know The Right Answers? Cheap Couches For Sale Under $200

  5. The vitality efficient dwelling enchancment credit.

  6. you are in point of fact a just right webmaster.
    The website loading speed is amazing. It kind of feels that you’re doing any distinctive
    trick. Moreover, The contents are masterpiece. you have done a great task in this matter!

  7. LeroyBak表示:

    online pharmacy india Order medicine from India to USA best online pharmacy india

  8. OHANA99表示:

    OHANA99เว็บสล็อตมากความสามารถที่ให้มากกว่าความสนุกสนาน

  9. Williamagric表示:

    buy drug online https://mexicanpharm24.cheap/# buying from online mexican pharmacy

  10. 5 Killer Quora Answers On Software For SEO Software for Seo

  11. Hello there, I believe your blog could be having browser compatibility
    issues. Whenever I take a look at your web site in Safari, it looks fine but when opening in I.E., it’s got some overlapping issues.
    I just wanted to provide you with a quick heads up!
    Other than that, wonderful site!

  12. Greetings! Very useful advice in this particular post! It is the little
    changes which will make the most important changes. Thanks a
    lot for sharing!

  13. slot gacor表示:

    There is certainly a lot to find out about this topic. I like all the points you made.

  14. Debra表示:

    10 Amazing Graphics About Misty Windows Repair misty windows repair near me (Debra)

  15. StephenDeery表示:

    This page truly has all the information I wanted about this subject and didn’t know who to ask.

    xxx

  16. Hi! I just wanted to ask if you ever have any problems with hackers?
    My last blog (wordpress) was hacked and I ended up losing months
    of hard work due to no back up. Do you have any solutions to protect against hackers?

  17. Coreycob表示:

    Comprehensive medicine resource. Get info now.
    order prednisone online
    Medication leaflet provided. Pill impacts described.

  18. DevonPhack表示:

    http://pharm24.pro/# best treatment for ed
    how to fix ed

  19. DevonPhack表示:

    https://mexicanpharm24.cheap/# purple pharmacy mexico price list
    best medicine for ed

  20. The 10 Most Scariest Things About Glass Repair Birmingham Window Companies In Birmingham

  21. LeroyBak表示:

    mexican rx online mexican drugs buying from online mexican pharmacy

  22. Alexis表示:

    Guide To Tots And Cots: The Intermediate Guide In Tots And Cots tots and cots [Alexis]

  23. Hey there this is somewhat of off topic but I was wanting to know if blogs use WYSIWYG editors or if you have to
    manually code with HTML. I’m starting a blog soon but have no coding skills so I wanted to get guidance from
    someone with experience. Any help would be enormously appreciated!

  24. Hello to every body, it’s my first pay a visit of this webpage; this
    weblog carries awesome and genuinely excellent material in favor of visitors.

  25. Williamagric表示:

    best treatment for ed http://pharm24.pro/# ed remedies

  26. 1win mexico表示:

    WOW just what I was looking for. Came here by searching for
    que es 1win

  27. slot gacor表示:

    That is a really good tip particularly to
    those fresh to the blogosphere. Brief but very accurate information… Thanks for sharing
    this one. A must read article!

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

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