How to Remove Duplicate values from an array of integers. Four solutions, 1 ) One brute-force method using a for loop, 2)sort and remove, 3) using JavaScript Objects and 4) Using ES6 / ES2015 Sets.
Original source
How to Remove Duplicate values from an array of integers. Four solutions, 1 ) One brute-force method using a for loop, 2)sort and remove, 3) using JavaScript Objects and 4) Using ES6 / ES2015 Sets.
Original source
21 responses to “Remove duplicates from array in Javascript | Algorithm Interview Question”
how _temp works here please anyone know
RU Indian?
In my interview I was asked to sort alphanumeric array [5,8,az,bt,68,gy,15]
This approach helps me to find easy way to find min and max, Thanks TechSith
var ar = [1,3,4,4,6,12,6,8,-9,1,3,2,23,7];
let b = […new Set(ar)];
b.sort((a,b)=>a-b);
let min = b[0];
let max = b[b.length-1];
console.log(min + ' '+ max);
what if you get [ [ 1, 7 ], [ 6, 2 ], [ 7, 1 ], [ 2, 6 ], [ 1, 7 ], [ 4, 4 ], [ 4, 4 ] ] and you want [ [ 1, 7 ], [ 6, 2 ], [ 7, 1 ], [ 2, 6 ], [ 4, 4 ] ]
G8.. i really needed this solution
Hello! Can anyone, please explain me how exactly _temp encounters the value, I didn't understand this piece. Thanks!
sir please make more interview programming question ,this helps me confident in the interview
that thing you did at 9:18 is f**** amazing!
Hey, I love your tutorials and the way you explain everything line by line. Liked, Subscribed and already shared your tutorials with my colleagues. Looking forward for your upcoming videos.
Just noticed,
Object.keys method provided all the string values in the array.
nicely explained, keep it up sir, thank you 👍👍👍👍🙂🙂🙂🙂
Very nice
In the first algorithm, you have the variable len defined globally, which is unnecessary (the same reason why we put let i = 0). It's better to write the for loop as this:
for(let i = 0, len = a.length; i < len; i++)
This way, a.length is accessed only once, which is good, but len is cleared from memory the moment the loop finishes.
Freaking amazing
thanks. your video easy to learn.
First of all why this fake accent
Really neat thing!!! Please do more such a practical puzzle pieces.
var array = [1, 1, 2, 3, 4, 4, 5];
var filtered = [];
for (var i = 0; i < array.length; i++) {
if (!filtered.includes(array[i])) {
filtered.push(array[i])
}
}
console.log(filtered)
Weird, when I do:
let a = [3, 2, 9, 2, 1, 3, 2, 4, 5];
console.log([…new Set(a)]);
I get a result of: [ 3, 2, 9, 1, 4, 5 ]
it doesn't sort for me unless I put .sort() in the a variable.
I love your videos, thank you for your time and effort. Removing a duplicate element from an array is a tricky thing.By using collection(in Javascript it is Set) it can be done easily.
this was awesome…