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,245,547 Responses

  1. AlbertDuM表示:

    Lisinopril 1st: lisinopril online purchase – cheapest Lisinopril

  2. Markdob表示:

    Всем привет! Подскажите, где найтиполезные статьи о недвижимости? Сейчас читаю – https://opalubki26.ru

  3. discuss表示:

    wonderful points altogether, you simply won a new reader.
    What might you recommend in regards to your publish that
    you just made some days ago? Any certain?

  4. First of all I want to say awesome 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 head prior to writing.
    I have had trouble clearing my mind in getting my thoughts out.
    I truly do take pleasure in writing but it just seems like the first 10 to 15 minutes are usually
    wasted just trying to figure out how to begin. Any recommendations or tips?
    Cheers!

  5. DarrenUnjuh表示:

    http://plavixclo.com/# Clopidogrel 75 MG price

  6. excellent publish, very informative. I wonder why the other experts of this sector do not realize this.
    You must continue your writing. I am confident, you’ve a huge
    readers’ base already!

  7. BernardMop表示:

    buy Lisinopril online buy Lisinopril online price of lisinopril 20 mg

  8. new allocated land from state are mostly Leasehold even houses

  9. Charlespax表示:

    Thought this piece was a gem passing it on to you http://led119.ru/forum/user/106711/

  10. BrandenSlomy表示:

    dapoxetine online: max pharm – priligy

  11. AlbertDuM表示:

    cheapest Lisinopril: buy Lisinopril 1st – lisinopril 125 mg

  12. Hello there! This is my first visit to your blog! We are a collection of
    volunteers and starting a new project in a community
    in the same niche. Your blog provided us valuable information to work on. You have done a extraordinary job!

  13. DarrenUnjuh表示:

    http://iverfast.com/# buy minocycline 50mg for humans

  14. Appreciate the comprehensive advice. For more, visit roof repair near me

  15. Nikefut表示:

    darknet marketplace deep web search https://darknetmarketstore.com/

  16. Lawyers need to keep their websites updated regularly—great tips available at seo for legal firms

  17. BernardMop表示:

    cytotec online cheapest cytotec order cytotec online

  18. JacobLoody表示:

    кухня на заказ недорого москва – Кухни на заказ по доступной цене для жителей Москвы.

  19. Just wanted to say how essential it is to use quality materials; they make all the difference in longevity commercial roofers

  20. BernardMop表示:

    Lisinopril 1st Lisinopril 1st lisinopril1st

  21. Dennis Hicks表示:

    When’s the best season to install a new roof metal roofers

  22. best selling表示:

    Today, I went to the beach front with my kids. I found a
    sea shell and gave it to my 4 year old daughter and said “You can hear the ocean if you put this to your ear.” She placed the shell to her ear and screamed.
    There was a hermit crab inside and it pinched her
    ear. She never wants to go back! LoL I know this is totally off topic but
    I had to tell someone!

    Feel free to visit my webpage … best selling

  23. Boekp Viral表示:

    Thank you for some other informative website. The
    place else could I get that kind of info written in such a perfect way?
    I’ve a challenge that I’m simply now operating on, and I have been on the glance
    out for such information.

  24. You really make it seem so easy with your presentation but I find this topic to
    be really something which I think I would never understand.

    It seems too complicated and extremely broad for me.
    I’m looking forward for your next post, I’ll try
    to get the hang of it!

  25. Superb Comment to Personal Site Remark

    Fantastic submission! I’m truly relishing the subject matter
    on this website. Secure you ever contemplated about obtaining into online gaming establishment casino games?

    Evolution Casino is a incredible site with a expansive selection of high-quality engaging table manager offerings.

    The entire venture is outstandingly absorbing and veritable, it feels similar to you’re exactly
    at the casino in the gaming.

    Assuming you’re eager concerning testing the offering out, I’d
    be extremely delighted to hand over the guidance internet address.
    The Evolution Gaming Platform offers a fantastic joining reward for new recent enthusiasts.
    It would be unquestionably valuable examining further should you’re desiring a modified cyberspace gambling adventure.

    Thankfulness one more time pertaining to the fantastic web
    publication information. Uphold with the the wonderful labor!

    Also visit my homepage gambling virtual reality

發佈留言

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