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,153,097 Responses

  1. Veleco Mobility Scooter For Sale Tools To Make Your Everyday Lifethe Only Veleco Mobility Scooter For
    Sale Trick Every Individual Should Learn veleco mobility scooter for sale

  2. 25 Shocking Facts About Slot Bonuses Slots With Bonuses

  3. See What Veleco Mobility Scooters Reviews Tricks The Celebs Are Using veleco mobility scooters reviews

  4. You’ll Never Be Able To Figure Out This Single Lightweight Stroller’s Secrets single lightweight stroller [http://www.mc-ala.Org]

  5. This Is A SEO Agency In Uk Success Story You’ll Never Imagine seo Agency birmingham

  6. Five Killer Quora Answers On Slot Sound Effects slot sound effects

  7. A little point, yet one that perhaps shows the subtle social distinction in between the words.

    Feel free to surf to my webpage https://nlifelab.org/bbs/board.php?bo_table=free&wr_id=2569044

  8. BomesJoype表示:

    Lorem Ipsum has been the industry’s standard dummy text https://stepsdown.cfd/giotmp of type and scrambled it to make a type specimen book.

  9. What Experts In The Field Would Like You To Learn replace lock on upvc door

  10. Hello my loved one! I want to say that this article is amazing, nice
    written and come with almost all important infos.
    I would like to see extra posts like this .

  11. Think You’re Perfect For Doing Semi Truck Lawsuit?

    Check This Quiz Semi truck accident lawyers

  12. 10 Beautiful Images To Inspire You About Motor Vehicle Legal motor vehicle Accident lawyers

  13. sex表示:

    Hey would you mind letting me know which webhost you’re using?
    I’ve loaded your blog in 3 different web browsers and I must say this blog loads a lot quicker then most.
    Can you recommend a good internet hosting provider at a reasonable price?
    Cheers, I appreciate it!

  14. Denisha表示:

    15 Documentaries That Are Best About ADHD Medication Titration adhd titration meaning (Denisha)

  15. Are You Responsible For An Semi Truck Law Budget?
    12 Top Notch Ways To Spend Your Money Semi truck accidents

  16. How To Get Better Results Out Of Your Motor Vehicle Attorney motor vehicle Accident lawsuits

  17. I couldn’t resist commenting. Very well written!

  18. fb8824619表示:

    No matter if some one searches for his essential thing,
    so he/she wants to be available that in detail, thus that thing is maintained
    over here.

  19. What’s The Current Job Market For Cheap Single Stroller Professionals Like?
    cheap single stroller

  20. TAMPUNG99表示:

    What’s up to all, the contents present at this site
    are really awesome for people knowledge, well, keep up the nice work fellows.

  21. Five Killer Quora Answers To Cheap Single Pushchair Cheap single pushchair

  22. What Is The Reason Why Cheap Nespresso Machine Are So Helpful When COVID-19 Is In Session nespresso espresso machines (http://www.buehnehollenthon.at)

  23. memek becek表示:

    It is appropriate time to make some plans for the future
    and it is time to be happy. I’ve read this post and if I could I
    desire to suggest you some interesting things or suggestions.

    Perhaps you could write next articles referring
    to this article. I want to read even more things
    about it!

  24. You’ll Be Unable To Guess Window Repair Near Me’s Tricks window repair near me – list-lane-2.blogbright.net,

  25. asbestos表示:

    An asbestos lawyer can assist victims file a lawsuit against their employers for exposure to the toxic mineral.

    A lawsuit could allow victims to recover reimbursement for medical bills, lost wages and past suffering and pain.

發佈留言

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