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,154,824 Responses

  1. Ten Ways To Build Your Slot Gaming Empire
    win At Slots (dagmarkucerova.blog.idnes.cz)

  2. How Motor Vehicle Lawyers Has Become The Most Sought-After Trend Of
    2023 Motor Vehicle accident attorneys

  3. 15 Best Coffee Beans 1kg Bloggers You Must Follow best coffee beans 1kg; https://57.glawandius.com,

  4. Unquestionably believe that that you said. Your favorite justification seemed to be
    on the internet the easiest thing to take into accout of. I say to you,
    I certainly get irked while other people think about concerns that they plainly do not understand
    about. You controlled to hit the nail upon the highest as well as outlined out the whole thing without having side effect , people can take
    a signal. Will probably be again to get more. Thank you

  5. I have fun with, result in I discovered just what
    I was looking for. You have ended my 4 day long hunt! God
    Bless you man. Have a great day. Bye

  6. aricept order表示:

    Meds prescribing information. What side effects?
    aricept order
    Actual trends of medicament. Get information here.

  7. The Most Common Adhd Symptoms Test Mistake Every Beginning Adhd Symptoms
    Test User Makes common Adhd Symptoms

  8. What’s The Job Market For Double Glazing Near Me
    Professionals? double glazing near me

  9. Meds prescribing information. What side effects?
    ipratropium generic
    Best information about pills. Get now.

  10. Birth Defect Attorney Explained In Fewer Than 140 Characters Birth Defect Law Firm

  11. Five Killer Quora Answers On Replacement Double Glazing Units Near Me double Glazing Units near me

  12. SEO_qvMa表示:

    продвижение сайтов в москве в топ https://prodvizhenie-sajtov-v-moskve115.ru .

  13. You’ll Be Unable To Guess Fela Federal Employers Liability Act’s Tricks
    fela Federal employers liability Act

  14. Jude表示:

    10 Things Everyone Has To Say About Demo Slot Zeus Vs Hades Rupiah Jude

  15. Hello just wanted to give you a quick heads up. The text in your post seem to
    be running off the screen in Opera. I’m not sure if this is a formatting
    issue or something to do with internet browser compatibility but I thought I’d post to let you know.
    The layout look great though! Hope you get the problem
    fixed soon. Cheers

  16. muncultoto表示:

    I was curious if you ever considered changing the page layout of your website?

    Its very well written; I love what youve got to say.
    But maybe you could a little more in the way of content so people could connect with it better.

    Youve got an awful lot of text for only having one or 2 images.
    Maybe you could space it out better?

  17. Meds information sheet. What side effects?
    can you buy generic rizatriptan tablets
    All what you want to know about medicament. Read now.

  18. Every weekend i used to pay a visit this site, for the reason that i
    want enjoyment, for the reason that this this website
    conations genuinely good funny data too.

  19. porn site表示:

    First of all I would like to say fantastic blog! I had a quick question that I’d like to ask if you don’t mind.
    I was curious to find out how you center yourself
    and clear your thoughts before writing. I have had a difficult time
    clearing my mind in getting my ideas out there.
    I truly do take pleasure in writing however it just seems like the first 10 to 15 minutes are
    wasted simply just trying to figure out how to begin. Any recommendations or hints?
    Kudos!

  20. Medication prescribing information. What side effects?
    can i get cheap phenergan
    Everything what you want to know about medicine. Get information now.

  21. cliphotvn表示:

    This design is wicked! You definitely know how to keep
    a reader amused. Between your wit and your videos, I was almost moved to start my
    own blog (well, almost…HaHa!) Wonderful job. I really enjoyed what you had to say, and more than that,
    how you presented it. Too cool!

  22. 10 Unexpected Slot Apps Tips Top-rated slots (http://Www.google.com)

  23. 14 Smart Strategies To Spend Extra Best Slot Machines Budget multi-line slots

  24. Medicine information for patients. What side effects can this medication cause?
    can you get cheap nolvadex without a prescription
    Some about medication. Get information here.

  25. See What Online Charity Shop Uk Clothes Tricks The Celebs Are Using online charity shop uk Clothes

  26. SEO_ulMa表示:

    продвижение сайтов в москве в топ https://www.prodvizhenie-sajtov-v-moskve115.ru .

  27. Think You’re Ready To Start High Wycombe Windows And Doors?
    Do This Test Front Doors High Wycombe

發佈留言

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