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,230,128 Responses

  1. Derekfar表示:

    ultrabet bonus: ultrabet – ultrabet guncel

  2. This post is in fact a good one it assists new
    internet viewers, who are wishing for blogging.

  3. ClydePab表示:

    deneme bonusu veren siteler yeni: deneme bonusu veren siteler yerliarama.org – deneme bonusu veren siteler mycbet.com

  4. It’s amazing in favor of me to have a website,
    which is helpful for my knowledge. thanks admin

  5. Derekfar表示:

    az parayla cok kazandiran slot oyunlar?: en cok kazand?ran slot oyunlar? – en kazancl? slot oyunlar?

  6. service表示:

    A Step-By-Step Guide To Replace Volvo Key From Start To Finish service

  7. Guide To Electric Patio Outdoor Heater: The Intermediate Guide To
    Electric Patio Outdoor Heater electric patio outdoor heater

  8. and you’ll likely see the age-old story of the clueless husband and the frustrated wife.エロ 人形It’s easy to understand why negativity in relationships is accepted as a normal part of life,

  9. How Oak Electric Fireplace Suite Is A Secret Life Secret Life
    Of Oak Electric Fireplace Suite free standing Electric fire suites

  10. 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 extremely broad for me.
    I’m looking forward for your next post, I’ll try to get the hang of it!

  11. Why Window And Door Doctor Could Be More Dangerous Than You Realized home Window repair

  12. LarryCoell表示:

    deneme bonusu veren siteler deneme bonusu veren siteler yerliarama.org denemebonusuverensiteler.top

  13. Тут можно преобрести огнеупорные сейфы огнестойкий сейф купить

  14. An outstanding share! I have just forwarded this onto a co-worker who had been conducting a
    little research on this. And he actually bought me lunch simply because I stumbled upon it for him…

    lol. So allow me to reword this…. Thank YOU for the meal!!
    But yeah, thanks for spending some time to discuss this matter here on your blog.

    my homepage; best platform to monetize blog

  15. Thank you for the auspicious writeup. It in fact was a amusement account it.
    Look advanced to more added agreeable from you! By the way, how can we communicate?

    my webpage advertisement platforms

  16. Roma99 login表示:

    I would like to thank you for the efforts you’ve put in penning
    this site. I’m hoping to see the same high-grade content from you in the future as well.
    In truth, your creative writing abilities has inspired me to get my own,
    personal website now 😉

  17. scnlog表示:

    Hi superb website! Does running a blog such as this require a great deal
    of work? I have virtually no expertise in computer programming however I was hoping to start my own blog soon. Anyhow, if you have any suggestions or techniques for new blog owners please share.
    I understand this is off topic but I just needed to ask.
    Many thanks!

  18. Nikefut表示:

    darkmarkets deep dark web darkmarkets

  19. Derekfar表示:

    deneme bonusu veren siteler: deneme bonusu veren siteler betturkey – deneme bonusu veren siteler yerliarama.org

  20. A Trip Back In Time What People Said About Sports Toto 4d Latest Result 20 Years Ago 토토사이트 모음

  21. Reprogrammed表示:

    The 10 Most Scariest Things About Audi Key Reprogrammed

  22. Responsible For The Replace Upvc Window Handle Budget?
    10 Very Bad Ways To Invest Your Money upvc handle window [vuf.minagricultura.gov.co]

  23. Medicine information sheet. Long-Term Effects.
    how to get generic ramipril online
    All information about drugs. Read here.

  24. Undeniably believe that which you stated. Your favorite justification appeared to be on the web the easiest thing to be aware of.

    I say to you, I definitely get irked while people think about worries that they just do not
    know about. You managed to hit the nail upon the top as well as
    defined out the whole thing without having side-effects
    , people can take a signal. Will probably be back to get more.

    Thanks

    http://ww7.livesgp.casa/

  25. Тут можно преобрести сейф купить для ружья шкафы для оружия

發佈留言

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