Infosys interview question

Getting unique elements from the array?

Interview Answers

Anonymous

Sep 30, 2019

Using Set merthod with spread operator

3

Anonymous

Jan 6, 2021

var arr = [1,4,7,3,1,7,9,5]; var uniqueArray = [...new Set(arr)] console.log(uniqueArray);

2

Anonymous

Jul 30, 2022

A: We can use the native filter method of an Array in the following way to get an array with unique values: Example: -------- function onlyUnique(value, index, self) { return self.indexOf(value) === index; } // usage example: var a = ['a', 1, 'a', 2, '1']; var unique = a.filter(onlyUnique); console.log(unique); // ['a', 1, 2, '1'] Output: ------- [ "a", 1, 2, "1" ]