Answers for "a method that takes an integer array argument and removes all duplicates from the array in Java"

1

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] + " ");
      }
   }
}
Posted by: Guest on October-30-2020
-1

array remove duplicate in java

List<Integer>numbers = Arrays.asList(1,2,2,2,3,5); // [1, 2, 3, 5]
System.out.println(numbers);

Set<Integer> hashSet = new LinkedHashSet(numbers);
ArrayList<Integer> removedDuplicates = new ArrayList(hashSet);
System.out.println(removedDuplicates); // [1, 2, 3, 5]
Posted by: Guest on June-04-2021

Code answers related to "a method that takes an integer array argument and removes all duplicates from the array in Java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language