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. 

How to sort a list of objects in java 8 with stream?

 In Java 8, you can use the Stream API to sort a list of Employee objects based on name and ID. Here's an example:

java
import java.util.ArrayList; 
import java.util.Comparator; 
import java.util.List; 
class Employee
private int id; 
private String name;
public Employee(int id, String name) {
this.id = id; 
this.name = name;
 }
public int getId()
return id; 
 } 
public String getName()
return name; 
 } 
@Override public String toString() {
return "Employee{" + "id=" + id + ", name='" + name + '\'' + '}'
 } 
public class EmployeeSortingExample {
public static void main(String[] args)
 List<Employee> employees = new ArrayList<>(); 
 employees.add(new Employee(3, "John")); 
 employees.add(new Employee(1, "Alice")); 
 employees.add(new Employee(2, "Bob")); 
// Sort by name List<Employee> sortedByName = employees.stream() .sorted(Comparator.comparing(Employee::getName)) .toList();
 System.out.println("Sorted by name:");
 sortedByName.forEach(System.out::println);
// Sort by ID List<Employee> sortedById = employees.stream() .sorted(Comparator.comparingInt(Employee::getId)) .toList(); System.out.println("Sorted by ID:"); sortedById.forEach(System.out::println); 
 } 
}

In this example, we use the stream() method to convert the List of employees into a stream. Then we use the sorted method along with a comparator created using Comparator.comparing() to specify the property to sort on (Employee::getName for name and Employee::getId for ID).

The sorted method returns a new sorted stream, and we use the toList method to collect the sorted elements into a new List.

After sorting the streams and collecting the sorted elements into new lists, we use the forEach method to iterate over the sorted lists and print the employees' details. The System.out::println method reference is used as a lambda expression to print each employee.