除了双循环这种,请问各位大神还有别的好方法吗?先谢过为敬
|
1
为什么要双循环呢 第一次循环的时候如果你是往 map 里扔已经知道谁是重复的了
|
|
LonelyJan 8, 2017
2
1 楼说的对
|
|
slixurdJan 8, 2017
3
只有一个重复元素的话就是经典算法题吧
反正就是空间 /时间看需要选哪个.... 不需要考虑空间就用 Map 吧.简单粗暴 |
|
pupbossJan 8, 2017
4
如果数据量大,可以用 dict ,用空间换时间
|
|
vchatJan 8, 2017
5
如果可以的话 往 set 集合里面放, 然后判断返回值 然后将重复元素放在 arraylist 里
|
|
palmersJan 8, 2017
6
同意#5 的做法
|
|
skywaymanJan 8, 2017
7
最终是想找出,还是去掉多余重复的? 前者循环一次,MAP<Key,Count>计数就好,后者 Set 一次转换行. 如果使用 Stream,还可以在多 CPU 上开 Parallel.
|
|
8
|
|
9
@murmur 请问是类似这个思路吗?
@Lonely 请问是类似这个思路吗? @skywayman 请问是类似这个思路吗? public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add("string1"); list.add("string1"); list.add("string1"); list.add("string1"); list.add("string3"); list.add("string2"); list.add("string2"); HashMap<String, Integer> hashMap = new HashMap<String, Integer>(); for (String string : list) { if (hashMap.get(string) != null) { Integer value = hashMap.get(string); hashMap.put(string, value+1); } else { hashMap.put(string, 1); } } for (Entry<String, Integer> entry : hashMap.entrySet()) { String key=entry.getKey(); Integer value= entry.getValue(); if (value>1) { System.out.println("the element:"+key+" is repeat"); } } } |
|
14
没记错的话,有个方法是排序然后遍历……?
如果允许额外空间,用 map |
|
16
list.removeAll(set(list))
|
|
koozeJan 9, 2017
17
rx
|
|
bhagavadJan 9, 2017
18
时间有要求还是空间有要求? ArrayList 中元素是什么类型?这些前提都得说清。
|
|
swim2sunJan 9, 2017
21
Java8 可以这样写:
List<String> list = Arrays.asList("1", "2", "3", "3"); Map map = list.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); |
|
webcoderJan 9, 2017
22
简单点,把一个数组 join 成一个字符,然后循环 Contains 一下。
局限点,不适用于对象数组。 注意点,需要作好数据间隔符,防止拼接后数据混乱。 |
|
vchatJan 9, 2017
23
@Adia 使用 hashset #add 方法 如果是重复的元素则返回值为 fase [HashSet#add]( http://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html#add(E))
注意: 添加元素为非基本类型(包装类型)或 String 则需要实现 equals 和 hashCode 方法确保元素唯一性 |
|
syasukerJan 9, 2017
25
也许可以 Java8 中使用 Stream 流思想进行过滤之类的
|
|
zhidianJan 9, 2017
27
for str in list:
if (str not in set) { set <- str } else { // dup } |
|
snnnJan 9, 2017 via Android
28
sort
|
|
29
先排序后便利,时间复杂度 n log ( n )
|
|
PerryJan 9, 2017 via iPhone
30
优雅到底是什么意思?
|
|
31
同意楼上,先排序后再遍历,与上个元素比对; Java Stream 有去重函数可以方便很多~
|
|
lovekohnJan 9, 2017
32
排序后,遍历下相邻的两个是否相同
|
|
fffflyfishJan 9, 2017
33
一楼说的对,这种时候就该用 hashmap,时间复杂度就是 O(n)
|
|
fffflyfishJan 9, 2017
34
如何,够不够优雅 |
|
35
// Guava
List<Employee> employees = // ... Multimaps.index(employees, e -> e.getName()) // Guava 另外一个版本 List<Employee> employees = // ... Collection<Employee> filtered = Collections2.filter(employees, new Predicate<Employee>() { @Override public boolean apply(Employee e) { return e.getName().equals("John Doe"); } }); // if you want a List: List<Employee> filteredList = new ArrayList<>(filtered); // Java 8 List<Employee> filteredList = employees.stream() .filter(e -> e.getName().equals("John Doe")) .collect(Collectors.toList()); |
|
mingl0280Jan 10, 2017
36
//C#奇技淫巧
ArrayList b = new ArrayList(); Dictionary<int, int> x = new Dictionary<int, int>(); foreach (int elem in a) { try { x.Add(elem, 0); }catch(Exception){ b.Add(elem); } } |