Java
Write a method that uses an ArrayList and its contains method to take an input array of strings return a new array, free of any duplicate strings.
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
public class ArrayStrings {
public static void main(String[] args) {
List<String> alist1 = new ArrayList<String>();
alist1.add("sneha");
alist1.add("shweta");
alist1.add("Marry");
alist1.add("sneha");
alist1.add("Marry");
System.out.println("The ArrayList items are:" );
for (String i : alist1)
System.out.println(i);
HashSet<String> h = new HashSet<String>(alist1);
List<String> alist2 = new ArrayList<String>(h);
System.out.println("\nThe new arraylist items after removing duplicates are:" );
for (String i : alist2)
System.out.println(i);
}
}
Get Answers For Free
Most questions answered within 1 hours.