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...

846,834 Responses

  1. %%

    Feel free to surf to my blog post … 300Mg vegan cbd gummies

  2. 人妻A片表示:

    Way cool! Some very valid points! I appreciate you writing this article and
    the rest of the site is also very good.

    Also visit mmy blog: 人妻A片

  3. empresa表示:

    I do consider all of the ideas you have introduced in your post.
    They’re really convincing and will definitely work. Nonetheless,
    the posts are very short for starters. May you please prolong them a little
    from subsequent time? Thanks for the post.

  4. This paragraph provides clear idea in favor of the new users
    of blogging, that genuinely how to do blogging and site-building.

    Also visit my blog – โบด๊อก คาสิโน

  5. Jancorce表示:

    cannazon market darknet tor dark web

  6. Damontriep表示:

    tor market links 2022 cannahome link

  7. Damontriep表示:

    black market websites tor marijuana dark web

  8. %%

    Feel free to visit my web blog: Cbd Sweets benefits

  9. Damontriep表示:

    dark markets czech republic buds express

  10. Beth表示:

    %%

    Feel free to visit my homepage: sexdolls for sale (Beth)

  11. Thanks on your marvelous posting! I actually enjoyed reading it, you might be a great author.
    I will be sure to bookmark your blog and will come back sometime
    soon. I want to encourage you continue your great job,
    have a nice evening!

  12. THC gummies paris isolate is pure THC,
    without various other cannabinoids.

  13. What’s up Dear, are you actually visiting this web page regularly, if so after that you will definitely
    get fastidious knowledge.

  14. %%

    Feel free to visit my web page … sexdolls For sale

  15. PatrickCrats表示:

    dark net market list reddit 0day onion

  16. You actually make it seem so easy with your presentation but I find this topic to be actually something that I think
    I would never understand. It seems too complicated and very broad for me.
    I am looking forward for your next post, I’ll try to get
    the hang of it!

  1. 2022-09-24

    […] Focus Q Elite even months, you drag yourself to. Exhausted, it becomes easier to invite colds and Focus Q Elite Reviews illness. Perhaps you took just a little cold medicine here and there, holiday no going to bed. No […]

發佈留言

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