Skip to content

Instantly share code, notes, and snippets.

View chygoz2's full-sized avatar

Chigozie Ekwonu chygoz2

View GitHub Profile
@chygoz2
chygoz2 / gist:efd264cd41baa4e820bbbb3e2d6e9895
Created October 18, 2018 08:53
Gist demonstrating the use of Promises and async/await to handle asynchronous operations in loops
import axios from 'axios'
const API_ENDPOINT = 'https://sample-flights-api.com/cities/'
async function getAirports (cityCodes) {
let promises = []
let airports = []
cityCodes.forEach(city => {
let promise = new Promise(async (resolve, reject) => {
try {
let { data } = await axios.get(`${API_ENDPOINT}city`)
static int sockMerchant(int n, int[] ar) {
Set set = new HashSet<Integer>();
int noOfPairs = 0;
for (int i=0; i<n; i++) {
if (set.contains(ar[i])) {
noOfPairs++;
set.remove(ar[i]);
} else {
set.add(ar[i]);
}
@chygoz2
chygoz2 / removeInstance.js
Last active April 24, 2021 00:30
Algo friday 2 solution
function removeInstance(nums, val) {
if (!Array.isArray(nums)) throw new Error('Invalid input')
return nums.filter(entry => entry !== val).length
}
@chygoz2
chygoz2 / startAndEnd.js
Last active April 27, 2021 13:01
Algo friday 3 solution
const startAndEnd = (nums, val) => {
if (!Array.isArray(nums) || typeof val !== 'number') throw new Error('Invalid input')
const min = Math.min(...nums);
let positions = [-1, -1]
let countLessThanVal = 0
let countOfVal = 0
let countOfMin = 0
@chygoz2
chygoz2 / arrayProduct.js
Last active May 2, 2021 15:43
Algorithm challenge week 4 solution
const arrayProduct = nums => {
if(!Array.isArray(nums)) throw new Error('Invalid input')
let product = 1
let countOfZeros = 0;
let productWithoutZeros = 1
for (let i = 0, j = nums.length - 1; i <= j; i++, j--) {
if (typeof nums[i] != 'number' || typeof nums[j] != 'number') throw new Error('Invalid input')
@chygoz2
chygoz2 / mergeAges.ts
Last active May 13, 2021 18:20
Algorithm Fridays 5 solution
const mergeAges = (classA: Array<number>, classB: Array<number>) => {
let mergedAges: Array<number> = []
let i = 0, j = 0
while(i < classA.length && j < classB.length) {
if (isNaN(classA[i]) || isNaN(classB[j])) throw new Error('Invalid input')
if (classA[i] <= classB[j]) {
mergedAges.push(classA[i++])
@chygoz2
chygoz2 / shuffleClass.js
Last active May 16, 2021 18:43
Algorithm week 6 challenge
const shuffleClass = (classList, count) => {
if (isNaN(count) || !Array.isArray(classList)) throw new Error('Invalid input ');
let newClassList = [];
if (count === 0 || Math.abs(count) >= classList.length) return classList;
if (count > 0) {
for (let i = classList.length - count; i < classList.length; i++) {
newClassList.push(classList[i]);
@chygoz2
chygoz2 / Program.cs
Last active May 28, 2021 17:03
Algorithm week 7 challenge
using System;
using System.Linq;
namespace minimumDifference
{
class Program
{
public static void Main(string[] args)
{
Console.WriteLine(Program.getMinimumDifference(new string[] { "16:15", "16:00", "12:20" }));
@chygoz2
chygoz2 / Program.cs
Created May 30, 2021 07:12
Algorithm week 8 challenge
using System;
using System.Collections.Generic;
namespace NumberCombinations
{
class Program
{
static void Main(string[] args)
{