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,231,527 Responses

  1. Ronaldpsymn表示:

    https://cytotec.club/# cytotec online

  2. Davidtes表示:

    lisinopril 20 mg canadian zestoretic 20 25 mg price of zestril

  3. 10 Wrong Answers To Common What CSGO Cases Have Butterfly Knives Questions Do You Know The
    Right Answers? operation Riptide Case

  4. Davidtes表示:

    ciprofloxacin generic price cipro online no prescription in the usa ciprofloxacin

  5. 15 Unquestionably Good Reasons To Be Loving 18-Wheeler Accident Attorney Near Me 18 Wheeler Accident Lawsuits

  6. Davidtes表示:

    cheapest price for lisinopril lisinopril 10mg tablet lisinopril 20mg prices

  7. Davidtes表示:

    cipro for sale ciprofloxacin 500mg buy online buy cipro online

  8. Ronaldpsymn表示:

    https://ciprofloxacin.tech/# п»їcipro generic

  9. 10 Things We All Do Not Like About Key Car Replacement lost car keys replacement cost uk

  10. Repairs To Upvc Windows Explained In Less Than 140 Characters window repairs

  11. In fact no matter if someone doesn’t know after that its
    up to other people that they will help, so here it takes place.

  12. Davidtes表示:

    propecia generic buying propecia without a prescription cost of propecia

  13. My brother recommended I might like this web site.
    He was totally right. This post actually made my day.
    You can not imagine simply how much time I had spent for this information!
    Thanks!

  14. 20 Insightful Quotes On Espresso Machines coffee for espresso machine

  15. Ronaldpsymn表示:

    https://nolvadex.life/# tamoxifen hair loss

  16. WilliamRhype表示:

    http://lisinopril.network/# price of lisinopril 5mg

  17. WilliamRhype表示:

    https://cytotec.club/# buy cytotec pills

  18. Vacuum Mops表示:

    Learn About Robot Vacuum’s While Working From The Comfort Of Your Home Vacuum Mops

  19. Five Things You’re Not Sure About About Saab 9-3 Replacement Key lost saab keys

  20. How To Create An Awesome Instagram Video About Wall Mounted Electric Fire Electric Wall Fireplace

  21. Jame表示:

    10 Ways To Create Your Treadmill Used For Sale Empire treadmill for sale (Jame)

  22. Davidtes表示:

    lisinopril sale buy lisinopril 10 mg online lisinopril 20mg discount

  23. How Double Glazed Door Repairs Near Me Became The Top Trend On Social
    Media double glazing repair

  24. 10 Things That Your Family Taught You About Cerebral Palsy Lawsuit Cerebral Palsy Lawsuit

  25. Davidtes表示:

    lisinopril 40 mg tablet lisinopril average cost prinivil 20mg tabs

  26. teafish.cc表示:

    The 10 Scariest Things About Designer Handbags Black
    designer handbags black (teafish.cc)

  27. Davidtes表示:

    tamoxifen breast cancer prevention tamoxifen bone pain femara vs tamoxifen

  28. Davidtes表示:

    get cheap propecia tablets cost of generic propecia without dr prescription cost of cheap propecia without insurance

  29. It lets advertisers bid for commercials placed in sponsored hyperlinks of the search engine when someone
    searches on a key phrase that’s relevant to their
    enterprise. For this web page, we have constructed all links from high area authority websites
    and seen improvements when we construct the first link. You’ve gotten seen many emails coming by as spam
    day by day. Bending or V-like shapes might be achieved by incorporating double
    bonds, that can bear cis-trans isomerization in response to sure stimuli (typically irradiation with a suitable wavelength), as seen in numerous designs consisting of
    stilbene and azobenzene items. C6H5NH2. Consisting of a phenyl group (−C6H5) hooked up to an amino group (−NH2), aniline is the only aromatic amine.

    By regulation, any such amendment 1) is limited to a single subject, 2) should embody a ballot title
    not to exceed 15 phrases in length, 3) should additionally embody a ballot abstract to not exceed seventy five phrases in length, and 4) must additional include a monetary influence assertion to not exceed seventy five words in size..

    Except as noted beneath, all amendments proposed, regardless of the strategy of
    proposal, have to be permitted by 60 p.c of the voters in a referendum held simultaneously with the next normal election (that’s, the next one a minimum of ninety days after the modification is filed with the custodian of
    state data) earlier than they become part of the Constitution.

  30. 30 Inspirational Quotes For Double Glazed Units Near Me Double Glazed Windows – Talley-Bridges-2.Blogbright.Net

發佈留言

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