Skip to content

Instantly share code, notes, and snippets.

@masautt
masautt / bvl9h3f.js
Created September 6, 2019 17:57
How to find longest palindrome of a string in JavaScript?
var longestPalindrome = function(s) {
if (isPalindrome(s)) {
return s;
}
var maxP = s[0];
for (var i=0; i < s.length; i++) {
var curr = s[i];
for (var j= i + 1; j < s.length; j++) {
@masautt
masautt / uwq5qpf.js
Created September 6, 2019 18:20
How to remove duplicates from an array in JavaScript?
// Using Sets
function removeDupes(arr) {
return [...new Set(arr)];
}
// Using filter( )
function removeDupes1(arr) {
return arr.filter((v,i) => arr.indexOf(v) === i)
}
@masautt
masautt / luglhko.js
Created September 6, 2019 18:26
How to check if array contains a value in JavaScript?
console.log(["M", "A", "R", "E", "K"].includes("R")); // --> true
console.log(["M", "A", "R", "E", "K"].includes("S")); // --> false
@masautt
masautt / i9hzdt.js
Created September 6, 2019 18:30
How to check if all elements in an array are unique in JavaScript?
function hasDuplicates(array) {
return (new Set(array)).size !== array.length;
}
console.log(hasDuplicates(["M", "A", "S", "A", "U", "T", "T"])); // --> true
console.log(hasDuplicates(["M", "A", "R", "E", "K"])); // --> false
// https://stackoverflow.com/questions/7376598/in-javascript-how-do-i-check-if-an-array-has-duplicate-values
@masautt
masautt / 8osyb4f.js
Created September 6, 2019 18:42
How to find the largest value of an array in JavaScript?
function largestVal(arr) {
return Math.max(...arr)
}
console.log(largestVal([1,9,5,4,11,3,3,4,2,])); // --> 11
// https://www.jstips.co/en/javascript/calculate-the-max-min-value-from-an-array/
@masautt
masautt / v5jvz0e.js
Created September 6, 2019 18:43
How to find the smallest value of an array in JavaScript?
function smallestVal(arr) {
return Math.min(...arr)
}
console.log(smallestVal([1,9,5,4,11,3,3,4,2,])); // --> 1
// https://www.jstips.co/en/javascript/calculate-the-max-min-value-from-an-array/
@masautt
masautt / tb57y5j.js
Created September 6, 2019 18:47
How to find the intersection of 2 arrays in JavaScript?
function findIntersection(arr1, arr2) {
return arr1.filter(value => arr2.includes(value))
}
console.log(findIntersection(["M","A", "R", "E", "K"], ["M","A", "S", "A", "T", "T"])) // --> [ 'M', 'A' ]
// https://stackoverflow.com/questions/1885557/simplest-code-for-array-intersection-in-javascript
@masautt
masautt / 86526vu.js
Created September 6, 2019 18:53
How to remove all odd numbers from an array in JavaScript?
function removeOdds(arr){
return arr.filter(function (num) {
return num % 2 === 0;
});
}
console.log(removeOdds([8,6,7,5,3,0,9])); // --> [ 8, 6, 0 ]
// https://stackoverflow.com/questions/18305431/how-to-remove-all-odd-numbers-in-an-array-using-javascript
@masautt
masautt / 6x0juy.js
Created September 6, 2019 18:55
How to remove all even numbers from an array in JavaScript?
function removeEvens(arr){
return arr.filter(function (num) {
return num % 2 !== 0;
});
}
console.log(removeEvens([8,6,7,5,3,0,9])); // --> [ 7, 5, 3, 9 ]
// https://stackoverflow.com/questions/18305431/how-to-remove-all-odd-numbers-in-an-array-using-javascript
@masautt
masautt / q2b43jv.js
Created September 6, 2019 19:00
How to find the first duplicate element of an array in JavaScript?
function firstDupe(arr) {
return arr.findIndex((item, index) => arr.lastIndexOf(item) !== index)
}
console.log( firstDupe([8,6,7,5,3,0,9])); // --> -1 = !exist
console.log( firstDupe(["L", "E", "T", "T", "E", "R"])); // --> 1 = E
// https://stackoverflow.com/questions/39346182/javascript-how-to-find-first-duplicate-value-and-return-its-index