diff --git a/ListaUtils.java b/ListaUtils.java new file mode 100644 index 0000000..d902d75 --- /dev/null +++ b/ListaUtils.java @@ -0,0 +1,59 @@ +import java.util.ArrayList; +import java.util.HashSet; +//testando git com o joao +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..6ca7d04 --- /dev/null +++ b/Main.java @@ -0,0 +1,47 @@ +import java.util.ArrayList; +//teste git joao +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); + } +}