I have an Array from which i want to remove Duplicate items.
for(int data1=startpos;data1<=lastrow;data1++) {
String movie_soundtrk=cells.getCell(data1,Mmovie_sndtrk_cl).getValue().toString();
al.add(movie_soundtrk);
}
String commaSeparated=al.toString();
String [] items = commaSeparated.split(",");
String[] trimmedArray = new String[items.length];
for (int i = 0; i < items.length; i++) {
trimmedArray[i] = items[i].trim();
}
Set<String> set = new HashSet<String>();
Collections.addAll(set, trimmedArray);
System.out.println(set);
But this is not giving me unique Values from Array.
My Array:- {English, French, Japanese, Russian, Chinese Subtitles,English, French, Japanese, Russian, Chinese Subtitles}
Out Put :- [Japanese, Russian, French, Chinese Subtitles], Chinese Subtitles, [English, English]
You can find the distinct values in an array using the Distinct function. The Distinct function takes the array as an input parameter and returns another array that consists only of the unique, or non-duplicate, elements.
In Excel, there are several ways to filter for unique values—or remove duplicate values: To filter for unique values, click Data > Sort & Filter > Advanced. To remove duplicate values, click Data > Data Tools > Remove Duplicates.
Finding the non repeating element in an array can be done in 2 different ways. Method 1: Use two loops, one for the current element and the other to check if the element is already present in the array or not. Method 2: Traverse the array and insert the array elements and their number of occurences in the hash table.
You can do it in one line in java 7:
String[] unique = new HashSet<String>(Arrays.asList(array)).toArray(new String[0]);
and shorter and simpler in java 8:
String[] unique = Arrays.stream(array).distinct().toArray(String[]::new);
HashSet will do the job.
You can try this:
List<String> newList = new ArrayList<String>(new HashSet<String>(oldList));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With