Skip to content

Instantly share code, notes, and snippets.

@allucardster
allucardster / getMissingNumbers.js
Last active November 26, 2020 17:31
Find missing numbers in an array
function getMissingNumbers(arr) {
arr.sort((a, b) => (a - b));
const min = arr[0];
const max = arr[arr.length - 1];
const all = [...Array(max).keys()].map(x => x + min);
return all.filter(value => !arr.includes(value));
}
@allucardster
allucardster / findMissingNumber.js
Last active November 26, 2020 16:24
Find the missing number in an array
function findMissingNumber(arr)
{
const n = arr.length + 1;
let num = ((n + 1) * n) / 2;
arr.forEach(val => {
num = num - val;
});
return num;
@allucardster
allucardster / App\Controller\Api\ProductController.php
Last active April 21, 2021 22:10
JMS Serializer Subscriber example with Symfony 4.4
<?php
namespace App\Controller\Api;
use App\Repository\ProductRepository;
use FOS\RestBundle\Controller\AbstractFOSRestController;
use FOS\RestBundle\View\View;
use Symfony\Component\HttpFoundation\Response;
use FOS\RestBundle\Controller\Annotations as Rest;
<?php
/*
Find the missing number in a range (array)
*/
function permMissingElem(array $arr) : int
{
$n = count($arr) + 1;
$num = (($n + 1) * $n) / 2;
foreach($arr as $val) {
<?php
/*
Find the number of carry operations when sum two numbers. For example
the carry operations for 66+75 is 2 because:
11
66
+75
–––
<?php
/*
Find value that occurs in odd number of elements.
*/
function oddOccurrencesInArray(array $arr) : int
{
foreach(array_count_values($arr) as $number => $count) {
if ($count % 2) {
return $number;
<?php
/*
Rotate an array to the right by a given number of steps.
*/
function cyclicRotation(array $arr, int $k): array
{
if (empty($arr)) {
return $arr;
}
<?php
/*
A binary gap within a positive integer N is any maximal
sequence of consecutive zeros that is surrounded by ones
at both ends in the binary representation of N.
Find longest binary gap from given N integer
*/
function longBinaryGap(int $a) : int
{
@allucardster
allucardster / colorsConfig.js
Created October 27, 2019 08:11
Create a colors config object from a `object[]` and `selected[string]` values
const colors = [
{
"color": "black",
"total": 99
},
{
"color": "brown",
"total": 93
},
{
window.onbeforeunload = function() {
if (data_needs_saving()) {
return "Do you really want to leave our brilliant application?";
} else {
return;
}
};