From 4f4b35902c1560af86b0381060e849c00a460358 Mon Sep 17 00:00:00 2001 From: artur plentz Date: Fri, 23 Aug 2024 19:14:44 -0300 Subject: [PATCH 1/2] Segundo commit --- ListaUtils.java | 59 +++++++++++++++++++++++++++++++++++++++++++++++++ Main.java | 47 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 ListaUtils.java create mode 100644 Main.java diff --git a/ListaUtils.java b/ListaUtils.java new file mode 100644 index 0000000..15aab6c --- /dev/null +++ b/ListaUtils.java @@ -0,0 +1,59 @@ +import java.util.ArrayList; +import java.util.HashSet; + +public class ListaUtils { + + public static int nOcorrencias(ArrayList l, Integer el) { + int count = 0; + for (Integer elemento : l) { + if (elemento.equals(el)) { + count++; + } + } + return count; + } + + public static boolean hasRepeat(ArrayList l) { + HashSet 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 l) { + HashSet uniqueElements = new HashSet<>(); + int repeatCount = 0; + for (Integer elemento : l) { + if (!uniqueElements.add(elemento)) { + repeatCount++; + } + } + return repeatCount; + } + + public static ArrayList listRepeat(ArrayList l) { + HashSet uniqueElements = new HashSet<>(); + HashSet repeatedElements = new HashSet<>(); + for (Integer elemento : l) { + if (!uniqueElements.add(elemento)) { + repeatedElements.add(elemento); + } + } + return new ArrayList<>(repeatedElements); + } + + public static ArrayList union(ArrayList l1, ArrayList l2) { + HashSet unionSet = new HashSet<>(l1); + unionSet.addAll(l2); + return new ArrayList<>(unionSet); + } + + public static ArrayList intersect(ArrayList l1, ArrayList l2) { + HashSet set1 = new HashSet<>(l1); + set1.retainAll(l2); + return new ArrayList<>(set1); + } +} diff --git a/Main.java b/Main.java new file mode 100644 index 0000000..65f8ef5 --- /dev/null +++ b/Main.java @@ -0,0 +1,47 @@ +import java.util.ArrayList; + +public class Main { + + public static void main(String[] args) { + ArrayList lista1 = new ArrayList<>(); + lista1.add(1); + lista1.add(2); + lista1.add(3); + lista1.add(2); + lista1.add(4); + lista1.add(5); + lista1.add(1); + + ArrayList lista2 = new ArrayList<>(); + lista2.add(4); + lista2.add(5); + lista2.add(6); + lista2.add(7); + lista2.add(2); + lista2.add(8); + + // a. Número de ocorrências do elemento 2 na lista1 + int ocorrencias = ListaUtils.nOcorrencias(lista1, 2); + System.out.println("Ocorrências do número 2: " + ocorrencias); + + // b. Verificar se há elementos repetidos em lista1 + boolean hasRepetidos = ListaUtils.hasRepeat(lista1); + System.out.println("Lista 1 tem elementos repetidos? " + hasRepetidos); + + // c. Número de elementos repetidos em lista1 + int numRepetidos = ListaUtils.nroRepeat(lista1); + System.out.println("Número de elementos repetidos em lista 1: " + numRepetidos); + + // d. Lista de elementos repetidos em lista1 + ArrayList repetidos = ListaUtils.listRepeat(lista1); + System.out.println("Elementos repetidos em lista 1: " + repetidos); + + // e. União das listas lista1 e lista2 + ArrayList uniao = ListaUtils.union(lista1, lista2); + System.out.println("União de lista 1 e lista 2: " + uniao); + + // f. Interseção das listas lista1 e lista2 + ArrayList intersecao = ListaUtils.intersect(lista1, lista2); + System.out.println("Interseção de lista 1 e lista 2: " + intersecao); + } +} From 21cc5f730320d405cf90c2f1a2798f9d36f5160d Mon Sep 17 00:00:00 2001 From: ArturPlentz2002 Date: Sat, 24 Aug 2024 17:06:34 -0300 Subject: [PATCH 2/2] testeJoao --- ListaUtils.java | 2 +- Main.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ListaUtils.java b/ListaUtils.java index 15aab6c..d902d75 100644 --- a/ListaUtils.java +++ b/ListaUtils.java @@ -1,6 +1,6 @@ import java.util.ArrayList; import java.util.HashSet; - +//testando git com o joao public class ListaUtils { public static int nOcorrencias(ArrayList l, Integer el) { diff --git a/Main.java b/Main.java index 65f8ef5..6ca7d04 100644 --- a/Main.java +++ b/Main.java @@ -1,5 +1,5 @@ import java.util.ArrayList; - +//teste git joao public class Main { public static void main(String[] args) {