
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Difference between two strings JavaScript
We are given two strings, say s and t. String t is generated by random shuffling string s and then add one more letter at a random position.
We are required to write a JavaScript function that takes both these strings and returns the letter that was added to t.
For example −
If the input stings are −
const s = "abcd", t = "abcde";
Then the output should be −
const output = "e";
because 'e' is the letter that was added.
Example
const s = "abcd", t = "abcde"; const findTheDifference = (s, t) => { let a = 0, b = 0; let charCode, i = 0; while(s[i]){ a ^= s.charCodeAt(i).toString(2); b ^= t.charCodeAt(i).toString(2); i++; }; b^=t.charCodeAt(i).toString(2); charCode = parseInt(a^b,2); return String.fromCharCode(charCode); }; console.log(findTheDifference(s, t));
Output
And the output in the console will be −
e
- Related Articles
- Finding shared element between two strings - JavaScript
- Hamming Distance between two strings in JavaScript
- Finding and returning uncommon characters between two strings in JavaScript
- Finding the difference between two arrays - JavaScript
- Finding the longest common consecutive substring between two strings in JavaScript
- Find the Symmetric difference between two arrays - JavaScript
- Difference between two times using Dayjs JavaScript library?
- Maximum absolute difference of the length of strings from two arrays in JavaScript
- Differences in two strings in JavaScript
- Similarities between different strings in JavaScript
- ASCII sum difference of strings in JavaScript
- Find the dissimilarities in two strings - JavaScript
- Twice join of two strings in JavaScript
- Construct objects from joining two strings JavaScript
- Finding gcd of two strings in JavaScript

Advertisements