Monday, February 19, 2018

Remove duplicates from an array in Javascript

Consider an array=[1,2,3,3,2,4,1,1]

We need to write a function to remove duplicates from an array.

function(arr){
let unique=[];

for(let i=0;i<arr.length;i++){

if(unique.indexOf(arr[i])== -1)
{
unique.push(arr[i]);
}
}
}
return unique;
}

No comments:

Post a Comment

Call a function in child component from parent component in React

Consider the following parent component class Parent extends React.Component { constructor(props) { super(props);         thi...