Skip to content

Instantly share code, notes, and snippets.

View marvuchko's full-sized avatar
Java

Marko Vučković marvuchko

Java
View GitHub Profile
@marvuchko
marvuchko / OrderBookRepositoryImpl.java
Created November 10, 2022 10:27
Single Implementation for Spring Data Repository and DDD repository
package pro.programista.infrastructure.jpa;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import javax.persistence.EntityManager;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
import org.springframework.stereotype.Repository;
import pro.programista.domain.primitives.PageResult;
@marvuchko
marvuchko / retry.interceptor.ts
Created July 13, 2022 10:38
Reatempts HTTP request on particular HTTP error status code.
import { Observable, of, throwError } from "rxjs";
import { concatMap, delay, retryWhen } from "rxjs/operators";
import { Injectable } from "@angular/core";
import {
HttpRequest,
HttpHandler,
HttpInterceptor,
HttpEvent
} from "@angular/common/http";
class Solution {
public int singleNumber(int[] nums) {
MultiValueMap<Integer, Integer> hash = new MultiValueMap<>();
for (int index = 0; index < nums.length; ++index)
hash.put(nums[index], index);
for (int index = 0; index < nums.length; ++index)
if (hash.get(nums[index]).size() == 1) return nums[index];
@marvuchko
marvuchko / Solution.java
Last active July 15, 2019 15:01
Leet Code. Missing element
class Solution {
public int missingNumber(int[] nums) {
int[] hash = new int[nums.length + 1];
for(int num : nums) // hashing the nums
++hash[num];
for(int i = 0; i < hash.length; ++i) // finding the missing value
if(hash[i] == 0) return i;
@marvuchko
marvuchko / Solution.java
Created July 9, 2019 14:35
Rotate Array for K positions
class Solution {
public void rotate(int[] nums, int k) {
int size = nums.length;
int[] rotated = new int[size];
for(int index = 0; index < size; index++) {
rotated[(index + k) % size] = nums[index];
}
for(int index = 0; index < size; index++) {
nums[index] = rotated[index];
}
@marvuchko
marvuchko / Solution.java
Created July 9, 2019 14:35
Rotate Array for K positions
class Solution {
public void rotate(int[] nums, int k) {
int size = nums.length;
int[] rotated = new int[size];
for(int index = 0; index < size; index++) {
rotated[(index + k) % size] = nums[index];
}
for(int index = 0; index < size; index++) {
nums[index] = rotated[index];
}
@marvuchko
marvuchko / Solution.java
Created July 4, 2019 09:38
LeetCode Maximum Depth of a binary tree
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
@marvuchko
marvuchko / Solution.java
Last active July 3, 2019 13:18
LeetCoder problem 169. Majority Element
class Solution {
private final int HASH_PRIME_FIRST = 54059;
private final int HASH_PRIME_SECOND = 76963;
int hashFunction(int key, int size) {
int hash = 37;
while(key > 0) {
hash += (hash * HASH_PRIME_FIRST) & (key * HASH_PRIME_SECOND);
key /= 10;