Find longest occurrence of same number in array

前端 未结 12 1435
一生所求
一生所求 2021-01-05 17:49

Using JavaScript, I\'m trying to find a way to find the longest occurrence of the same number (in this case, 1) in an array.

For instance, here\'s a sample array:

12条回答
  •  囚心锁ツ
    2021-01-05 18:15

    You will need another two arrays here.

    1. Store the distinct numbers from your source array using a loop
    2. Make a second set of array which is equal to the length of the first set of array which has the distinct numbers.
    3. Make a loop equal to the length of the first set of array and then push the values to the second set of array according to its index.
    4. Make a loop again using the second set of array and there you will find the most occurence using the index of the second array
    5. Finally, get from the first set of array the number using the index you got from step 4.

    I did not make the code for you to try it yourself first since you are asking only for some pointers

提交回复
热议问题