【发布时间】:2017-06-07 06:59:10
【问题描述】:
在我的应用程序中,我想从数组中获取第一个和重复的元素
例如,
String[] strings = {"one", "one", "one", "one", "one", "one", "one", "one", "one", "one", "one", "one"
, "two", "two", "two", "two", "two", "two", "two", "two", "two", "two", "two", "two", "two", "two", "two"
, "three", "three", "three", "three", "three", "three", "three", "three", "three", "three", "three"
, "four", "four", "four", "four", "four", "four", "four", "four", "four", "four"
, "five", "five", "five", "five", "five", "five"
, "six", "six", "six", "six", "six", "six", "six", "six", "six"
, "seven", "seven", "seven", "seven", "seven", "seven", "seven", "seven"
, "eight", "eight", "eight", "eight", "eight", "eight"
, "nine", "nine", "nine", "nine", "nine", "nine", "nine", "nine", "nine"
, "ten", "ten", "ten", "ten", "ten", "ten", "ten", "ten", "ten"};
对于上面的列表,我想得到类似的输出
一个
一个
两个
两个
三
三
四
四
五
五
六
六
七
七
八
八
九
九
十
十
【问题讨论】:
-
将所有值映射到 LinkedHashMap,然后将值添加回列表中,只是这次只添加第一个和最后一个重复项。
-
请向我们展示您到目前为止所做的尝试。我们不为您做您的(家庭)工作。
-
如果你只对元素感兴趣而不是你的第一个/最后一个元素的索引,基本上你只需将每个唯一元素打印两次,换句话说,这是没有意义的。你到底想达到什么目的?
-
为什么不是前两个重复元素?还是最后两个?还是中间两个?你怎么知道区别?你的问题毫无意义。