9. Palindrome Number

Solved at: Jul 23, 2025 🇲🇽

First/Ugly solution

6ms Runtime | Beats 68.49%

var isPalindrome = function(x) {
    const numberToString = String(x);
    const reversedString = [...numberToString].reverse().join("");
    if(reversedString !== numberToString) {
        return false
    } else return true
};
var isPalindrome = function(x) {
    const numberToString = String(x); // Store the number to string
    // Convert the string to an array, reverse it and we join it to a string again
    // This line right here wastes so much time, never fucking do this i'm sorry
    const reversedString = [...numberToString].reverse().join("");
    if(reversedString !== numberToString) { //finally we compare the two strings
        return false
    } else return true
};

Second/What I thought it would be a better solution but ended up performing even worse

8ms Runtime | Beats 48.30%

var isPalindrome = function(x) {
    const s = String(x);
    const length = s.length;
    let left = 0;
    let right = length -1;
    while(left <= right) {
        if(s[left] === s[right]) {
            left++
            right--;
        } else {
            return false;
        }
    }
    return true;
};

Why is this a worse solution?


/*
    My thinking process with this one was that we are using only one loop, instead of creating
    a string, the converting that to an array, then reversing, then creating a string out of it...
*/
var isPalindrome = function(x) {

    //We do convert this number to string, and here's were we fucked up
    //Alocating memory of this string, which can be a very VERY big one at that is wrong.
    const s = String(x);

    //The rest of this is actually very performant though.
    const length = s.length; // We get the length, we don't want to go to s.length on every loop
    let left = 0; //One pointer for left
    let right = length -1; //One pointer for right
    while(left <= right) { //If they meet, we finished and return true
        if(s[left] === s[right]) { //If they are the same we keep going
            left++
            right--;
        } else {
            return false;
        }
    }
    return true;
};

Third/The Best solution

5ms Runtime | Beats 79.57%


/*
    I'm actually impresed because while this is a just mathematical solution  which 
    doesn't require string convertion, it is just a little bit better than my first solution,
    so this is kinda overwhelming lmao. Sorry.
*/

var isPalindrome = function(x) {
    if (x < 0 || (x % 10 === 0 && x !== 0)) return false;
    let reversed = 0;
    while (x > reversed) {
        reversed = reversed * 10 + x % 10;
        x = Math.floor(x / 10);
    }
    return x === reversed || x === Math.floor(reversed / 10);
};

var isPalindrome = function(x) {
    if (x < 0 || (x % 10 === 0 && x !== 0)) return false; // Negative numbers or numbers ending with 0 (but not 0 itself) can't be palindromes
    let reversed = 0; // Store the reversed half of the number
    while (x > reversed) { // Only process until we've reversed half the digits
        reversed = reversed * 10 + x % 10; // Add the last digit of x to reversed
        x = Math.floor(x / 10); // Remove the last digit from x
    }
    return x === reversed || x === Math.floor(reversed / 10); // Check for even and odd length numbers
};