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,274,713 Responses

  1. Excellent article. I’m going through many of these issues
    as well..

    Feel free to surf to my website; realtor in ponce inlet fl

  2. I’m not that much of a online reader to be honest but your sites really nice, keep it up!
    I’ll go ahead and bookmark your website to come back later on. All the
    best

    Also visit my webpage :: realtor in volusia county

  3. Adeline表示:

    15 Startling Facts About Butt Plugs Shop You’ve Never Heard Of butt plugs and sex (Adeline)

  4. Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your point.
    You obviously know what youre talking about, why
    waste your intelligence on just posting videos to your weblog when you could be
    giving us something informative to read?

  5. CurtisIncix表示:

    pinup kazi: пин ап казино – pinup kazi

  6. Nikefut表示:

    darkmarket link dark web search engines darknet websites

  7. I think the admin of this web page is in fact working hard
    for his site, as here every information is quality
    based material.

  8. click site表示:

    It’s a shame you don’t have a donate button! I’d definitely donate
    to this excellent blog! I suppose for now i’ll settle for book-marking and adding your RSS feed to my
    Google account. I look forward to brand new updates and will
    share this website with my Facebook group. Talk soon!

  9. I’m not sure exactly why but this site is loading incredibly
    slow for me. Is anyone else having this issue or is it a problem on my end?

    I’ll check back later on and see if the problem still exists.

    Also visit my site; Wolvesbaneuo.Com

  10. HenrydeT表示:

    vavada vavada вавада

  11. DarioTob表示:

    пин ап казино: pinup kazi – pinup-kazi.ru

  12. Jamesdiure表示:

    вавада казино зеркало: vavada kazi – vavada-kazi.ru

  13. Jamesdiure表示:

    vavada-kazi.ru: vavada – vavada kazi

  14. Hello, I believe your web site may be having internet browser
    compatibility issues. Whenever I look at your website in Safari, it
    looks fine however, when opening in IE, it’s got
    some overlapping issues. I just wanted to provide you with a quick heads up!
    Other than that, excellent website!

    Here is my homepage – lottery numbers

  15. Загляните в наш магазин цветов в Темрюке!
    Здесь вас ждут стильные
    композиции для вашего праздника.

    У нас представлены цветы для
    ваших близких: от изящных цветочных наборов до шикарных букетов.

    Порадуйте любимых и близких свежими цветами.

    Аккуратная доставка по Темрюку сделает ваш
    заказ ещё более приятным. Оформляйте онлайн, и мы доставим букет
    по указанному адресу.

  16. I’ve been surfing online more than 3 hours today, yet I never found any interesting article like yours.
    It’s pretty worth enough for me. In my opinion, if all website owners and bloggers
    made good content as you did, the internet will be much more useful than ever
    before.

  17. Pol88表示:

    Hi friends, its enormous post concerning tutoringand entirely explained,
    keep it up all the time.

  18. I believe that is among the such a lot important info for me.
    And i’m happy studying your article. But should remark on few basic things, The web site style is wonderful, the
    articles is realtor in volusia county fl point of fact nice : D.
    Excellent job, cheers

  19. I’m not sure why but this website is loading very slow for me.
    Is anyone else having this problem or is it a problem on my end?

    I’ll check back later and see if the problem still exists.

  20. DarioTob表示:

    пин ап вход: пин ап зеркало – пин ап зеркало

  21. DarioTob表示:

    pinup: pinup kazi – пинап казино

  22. Fastidious answer back in return of this issue with solid
    arguments and explaining all concerning that.

  23. I am now not sure where you are getting your info, however good topic.
    I needs to spend a while studying more or working
    out more. Thank you for excellent information I was looking for this info for my mission.

    Stop by my blog post realtor in st augustine fl

  24. That is a great tip particularly to those
    new to the blogosphere. Short but very accurate information… Appreciate
    your sharing this one. A must read post!

    Feel free to visit my page … realtor in tucson arizona

  25. Genuinely when someone doesn’t know afterward its up to
    other viewers that they will assist, so here it takes place.

發佈留言

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