java remove equal element of a list
public static <T> List<T> removeDuplicates(List<T> list){
Set<T> set = new LinkedHashSet<>(list);
return new ArrayList<T>(set);
}
java remove equal element of a list
public static <T> List<T> removeDuplicates(List<T> list){
Set<T> set = new LinkedHashSet<>(list);
return new ArrayList<T>(set);
}
java remove duplicates
import java.util.*;
public class RemoveDuplicatesFromArrayList {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1,2,2,2,3,5);
System.out.println(numbers);
Set<Integer> hashSet = new LinkedHashSet(numbers);
ArrayList<Integer> removedDuplicates = new ArrayList(hashSet);
System.out.println(removedDuplicates);
}
}
how to remove duplicates from an array java
// Must sort arrays first --> Arrays.sort(arrayName)
public class RemoveDuplicateInArrayExample{
public static int removeDuplicateElements(int arr[], int n){
if (n==0 || n==1){
return n;
}
int[] temp = new int[n];
int j = 0;
for (int i=0; i<n-1; i++){
if (arr[i] != arr[i+1]){
temp[j++] = arr[i];
}
}
temp[j++] = arr[n-1];
// Changing original array
for (int i=0; i<j; i++){
arr[i] = temp[i];
}
return j;
}
public static void main (String[] args) {
int arr[] = {10,20,20,30,30,40,50,50};
int length = arr.length;
length = removeDuplicateElements(arr, length);
//printing array elements
for (int i=0; i<length; i++)
System.out.print(arr[i]+" ");
}
}
Remove duplicate element in an array in java
public class RemoveDuplicateElementDemo
{
public static int removeDuplicate(int[] arrNumbers, int num)
{
if(num == 0 || num == 1)
{
return num;
}
int[] arrTemporary = new int[num];
int b = 0;
for(int a = 0; a < num - 1; a++)
{
if(arrNumbers[a] != arrNumbers[a + 1])
{
arrTemporary[b++] = arrNumbers[a];
}
}
arrTemporary[b++] = arrNumbers[num - 1];
for(int a = 0; a < b; a++)
{
arrNumbers[a] = arrTemporary[a];
}
return b;
}
public static void main(String[] args)
{
int[] arrInput = {1, 2, 3, 3, 4, 5, 5, 6, 7, 8};
int len = arrInput.length;
len = removeDuplicate(arrInput, len);
// printing elements
for(int a = 0; a < len; a++)
{
System.out.print(arrInput[a] + " ");
}
}
}
remove duplicates from list java
ArrayList<Object> withDuplicateValues;
HashSet<Object> withUniqueValue = new HashSet<>(withDuplicateValues);
withDuplicateValues.clear();
withDuplicateValues.addAll(withUniqueValue);
remove duplicates from list java
Set<String> set = new HashSet<>(yourList);
yourList.clear();
yourList.addAll(set);
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us