remove repeated elements in array code example
Example 1: javascript remove duplicates from array
const initialArray = ['a', 'a', 'b',]
finalArray = Array.from(new Set(initialArray));
Example 2: javascript filter array remove duplicates
arrayWithOnlyUniques = [...new Set(arrayWithDuplicates)]
Example 3: 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);
for(int a = 0; a < len; a++)
{
System.out.print(arrInput[a] + " ");
}
}
}
Example 4: remove duplicates from array javascript
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}
var a = ['a', 1, 'a', 2, '1'];
var unique = a.filter(onlyUnique);
console.log(unique);
Example 5: array remove duplicates javascript
uniq = [...new Set(array)];