Get number of duplicates from ArrayList

前端 未结 3 380
自闭症患者
自闭症患者 2021-01-27 20:07

For example, say I have an ArrayList that could contain the following values:

x
x
x
y
y

Now what I want to retrieve is the number

3条回答
  •  日久生厌
    2021-01-27 20:25

    Well, for this what you can do is to traverse the list from the end. Keep a counter and keep checking for the change in the object. As soon as you detect a change, replace the number in the counter with the current position + 1. Do this till you reach the first position.

提交回复
热议问题