Friday, 16 June 2023

Find TwoSum from array in java

This one is very favourite question of java interviewers, In which you have to find the two numbers in an array that add up to a specific target sum, you can use the Two Sum problem solution. Here's an example implementation in Java:

java
import java.util.HashMap;
import java.util.Map; 
public class TwoSum
public static int[] findTwoSum(int[] nums, int target) { 
 Map<Integer, Integer> map = new HashMap<>(); 
for (int i = 0; i < nums.length; i++) { 
int complement = target - nums[i]; if (map.containsKey(complement)) { 
return new int[]{map.get(complement), i}; 
 } 
 map.put(nums[i], i); 
 }
return new int[]{-1, -1}; // If no solution is found 
 } 
public static void main(String[] args)
int[] nums = {2, 7, 11, 15}; 
int target = 9
int[] result = findTwoSum(nums, target); 
if (result[0] != -1 && result[1] != -1) { 
 System.out.println("Two numbers found: " + nums[result[0]] + " and " + nums[result[1]]); 
 } else
 System.out.println("No two numbers found that add up to the target sum."); } 
 } 
}

In this example, the findTwoSum method takes an array of integers (nums) and a target sum as parameters. It uses a HashMap to store the complement of each element (the difference between the target and the current number) and its corresponding index. It iterates through the array, checking if the complement exists in the HashMap. If found, it returns the indices of the two numbers that add up to the target sum. If no solution is found, it returns [-1, -1].

Best micro-service gateways available in spring boot application

 When it comes to choosing the best microservice gateway in a Spring Boot application, there are several popular options available. The choice depends on your specific requirements and preferences. Here are a few notable microservice gateway options for Spring Boot:

  1. Spring Cloud Gateway: Spring Cloud Gateway is a lightweight, developer-friendly gateway built on top of Spring Boot and Spring WebFlux. It provides a powerful routing and filtering mechanism, making it easy to build scalable and resilient microservice architectures. Spring Cloud Gateway integrates well with the Spring ecosystem and supports features like rate limiting, circuit breaking, and service discovery.

    <!-- Spring Boot Starter Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Spring Cloud Starter Gateway --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <!-- Additional Spring Cloud dependencies --> <!-- Add other Spring Cloud dependencies as per your requirements --> </dependencies><dependencies>
  2. Netflix Zuul: Netflix Zuul is a battle-tested microservice gateway developed by Netflix. While it's not explicitly built on Spring Boot, it can be used with Spring Cloud to leverage its features. Zuul offers robust routing and filtering capabilities, integrates with Netflix Eureka for service discovery, and supports features like dynamic routing, request/response modification, and centralized configuration.

    <!-- Spring Boot Starter Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Netflix Zuul Dependency --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency> <!-- Additional dependencies --> <!-- Add other dependencies as per your requirements --> </dependencies><dependencies>
  3. Kong: Kong is an open-source API gateway that can be used as a microservice gateway in Spring Boot applications. It provides features like routing, load balancing, authentication, rate limiting, and logging. Kong can be deployed as a standalone service or as part of a containerized infrastructure, and it offers various plugins to extend its functionality.<dependencies>

    <!-- Spring Boot Starter Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Kong Dependency --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-kong</artifactId> </dependency> <!-- Additional dependencies --> <!-- Add other dependencies as per your requirements --> </dependencies>
  4. Ambassador: Ambassador is a Kubernetes-native API gateway that can be used with Spring Boot microservices running on Kubernetes. It provides features like edge routing, traffic management, authentication, and observability. Ambassador integrates well with Kubernetes and can leverage its service discovery and routing capabilities.<dependencies>

    <!-- Spring Boot Starter Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Ambassador Dependency --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-kubernetes-ribbon</artifactId> </dependency> <!-- Additional dependencies --> <!-- Add other dependencies as per your requirements --> </dependencies>

These are just a few examples of microservice gateway options for Spring Boot. It's essential to evaluate your specific requirements, such as performance, scalability, security, and ease of integration, when choosing the best gateway for your project. Consider factors like community support, documentation, and compatibility with your existing infrastructure while making your decision.

Thursday, 15 June 2023

How to find the duplicate elements in java array ?

To find duplicate elements in an array in Java, you can use various approaches. Here are a few commonly used methods:

  1. Using Brute Force: You can use a nested loop to compare each element of the array with all other elements and check for duplicates. If a duplicate is found, you can store or print the element.

    java
    int[] array = {1, 2, 3, 4, 2, 5, 1}; for (int i = 0; i < array.length - 1; i++) { for (int j = i + 1; j < array.length; j++) { if (array[i] == array[j]) { System.out.println("Duplicate element: " + array[i]); } } }
  2. Using HashSet: You can utilize a HashSet to keep track of unique elements encountered in the array. When adding elements to the HashSet, if an element is already present, it indicates a duplicate.

    java
    int[] array = {1, 2, 3, 4, 2, 5, 1}; Set<Integer> uniqueElements = new HashSet<>(); Set<Integer> duplicateElements = new HashSet<>(); for (int num : array) { if (!uniqueElements.add(num)) { duplicateElements.add(num); } } System.out.println("Duplicate elements: " + duplicateElements);
  3. Using HashMap: You can utilize a HashMap to store the frequency of each element in the array. If the frequency exceeds 1, it indicates a duplicate element.

    java
    int[] array = {1, 2, 3, 4, 2, 5, 1}; Map<Integer, Integer> elementFrequency = new HashMap<>(); for (int num : array) { elementFrequency.put(num, elementFrequency.getOrDefault(num, 0) + 1); } for (Map.Entry<Integer, Integer> entry : elementFrequency.entrySet()) { if (entry.getValue() > 1) { System.out.println("Duplicate element: " + entry.getKey()); } }

These are a few approaches to find duplicate elements in an array in Java. Choose the one that suits your requirements and the size of the array.