forked from pucrs-gcs-es/1-pratica-git-github-basico
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListaUtils.java
More file actions
59 lines (52 loc) · 1.84 KB
/
Copy pathListaUtils.java
File metadata and controls
59 lines (52 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.util.ArrayList;
import java.util.HashSet;
public class ListaUtils {
public static int nOcorrencias(ArrayList<Integer> l, Integer el) {
int count = 0;
for (Integer elemento : l) {
if (elemento.equals(el)) {
count++;
}
}
return count;
}
public static boolean hasRepeat(ArrayList<Integer> l) {
HashSet<Integer> uniqueElements = new HashSet<>();
for (Integer elemento : l) {
if (!uniqueElements.add(elemento)) {
return true; // Se o elemento já estiver no HashSet, é repetido.
}
}
return false; // Se todos os elementos forem únicos.
}
public static int nroRepeat(ArrayList<Integer> l) {
HashSet<Integer> uniqueElements = new HashSet<>();
int repeatCount = 0;
for (Integer elemento : l) {
if (!uniqueElements.add(elemento)) {
repeatCount++;
}
}
return repeatCount;
}
public static ArrayList<Integer> listRepeat(ArrayList<Integer> l) {
HashSet<Integer> uniqueElements = new HashSet<>();
HashSet<Integer> repeatedElements = new HashSet<>();
for (Integer elemento : l) {
if (!uniqueElements.add(elemento)) {
repeatedElements.add(elemento);
}
}
return new ArrayList<>(repeatedElements);
}
public static ArrayList<Integer> union(ArrayList<Integer> l1, ArrayList<Integer> l2) {
HashSet<Integer> unionSet = new HashSet<>(l1);
unionSet.addAll(l2);
return new ArrayList<>(unionSet);
}
public static ArrayList<Integer> intersect(ArrayList<Integer> l1, ArrayList<Integer> l2) {
HashSet<Integer> set1 = new HashSet<>(l1);
set1.retainAll(l2);
return new ArrayList<>(set1);
}
}