Skip to content

Instantly share code, notes, and snippets.

View rohan-paul's full-sized avatar
🎯
Focusing

Rohan Paul rohan-paul

🎯
Focusing
View GitHub Profile
{
"results" : [
{
"address_components" : [
{
"long_name" : "1600",
"short_name" : "1600",
"types" : [ "street_number" ]
},
{
To store a grid of values, we have several options. We can use an array of row arrays and use two property accesses to get to a specific square, like this:
var grid = [["top left", "top middle", "top right"], ["bottom left", "bottom middle", "bottom right"]];
console.log(grid[1][2]);
// . bottom right
Or we can use a single array, with size width × height, and decide that
the element at (x,y) is found at position x + (y × width) in the array.
var Website = {
updatePageView: function() {
for(var i = 0; i < arguments.length; i++) {
this.pageViewNumber += arguments[i];
}
return this.pageViewNumber;
}
}
var profilePage = {
function myFunction(param) {
console.log(this + " says hello " + param);
}
myFunction.call("Paul", "world")
// Output => Paul says hello world
function myFunc() {
return Array.prototype.slice.call(arguments);
// args is now a real Array, so can use the sort() method from Array
}
console.log(myFunc('USA', 'UK', 'India'));
console.log(myFunc('USA', 'UK', 'India').sort());
// Will output =>
// [ 'USA', 'UK', 'India' ]
// [ 'India', 'UK', 'USA' ]
/*Problem - Given an array, find the int that appears an odd number of times. There will always be only one integer that appears an odd number of times.
Some examples -
[1] // => 1 (odd number of ones, no other numbers)
[1, 1, 2] // => 2 (even number of ones, odd number of twos)
[1, 1, 3, 5, 5] // => 3 (even number of ones and fives, odd number of threes)
[1, 2, 1, 2, 1] // => 1 (even number of twos, odd number of ones)
[1, 1, 2, 2] // => undefined behavior (no number with an odd number of occurrences)
[1, 2] // => undefined behavior (more than one number with an odd number of occurrences)*/
function findOdd(A) {
const findOdd = (xs) => (xs).reduce((a, b) => a ^ b);
// First generate a long enough array sequence to test the code with.
var generateArray = Array.from(new Array(100000),(val,index)=>index);
var startMyCode = Date.now();
function findOddMyCode(A) {
var len = A.length;
var A_sort = A.slice().sort();
var count = {};
/*Solution with time complexity of O(n^3). Cubic Algorithm.
Idea: For all pairs of integers, i ≤ j, check whether the sum of A[i..j] is greater than the maximum sum so far.*/
function findMaxSubArrayBruteForce(arr) {
var max_so_far = Number.NEGATIVE_INFINITY;
var leftIndex = 0,
rightIndex = arr.length - 1,
len = arr.length;
for (var i = 0; i < len; i++) {
var maxSequence = function(arr){
var curr_max = 0, max_so_far = 0;
for(var i = 0; i < arr.length; i++){
curr_max = Math.max(0, curr_max + arr[i]);
max_so_far = Math.max(curr_max, max_so_far);
}
return max_so_far;
}