Delete repeated items from a list

0

I am trying to do an exercise but I really do not know how to do the exercise using the class they are giving me, since the problem I have is with the use of IndexedList. If someone could give me some advice / clue or some example would be of great help.

 public static <E> IndexedList<E> deleteRepeated(IndexedList<E> l) {

 }// de indexedList 
    
asked by jeyXD 15.09.2018 в 17:41
source

2 answers

0

What you can do is create a new IndexedList and iterate over the original to check if each element exists in the new IndexedList and otherwise add it.

This can help you link

    
answered by 17.09.2018 в 06:28
0

I do not know the IndexedList class, but I'll give you an example of what you can do with Java8

//creamos la lista inicial
List<String> lista = new ArrayList<>();
lista.add("cadena1");
lista.add("cadena2");
lista.add("cadena1");
lista.add("cadena3");
lista.add("cadena1");
//en este punto nuestra lista tiene los siguientes elementos [cadena1,cadena2,cadena1,cadena3,cadena1 ]

//la magia comienza aca
//pasamos la lista a un stream ya que nos ofrece el metodo distinct el cual elimina los duplicados y retorna un stream
//luego agrupamos el stream y lo volcamos en una lista nuevamente.
lista = lista.stream().distinct().collect(Collectors.toList());

//imprimimos la lista utilizando la referencia al metodo println
lista.forEach(System.out::println);

Greetings

    
answered by 17.09.2018 в 22:05