約 15,500 件の結果
リンクを新しいタブで開く
  1. Detecting duplicates in an array is a common task in Java, and it can be done efficiently using modern approaches. Below are two widely used and optimal methods.

    Using HashSet

    Steps:

    1. Create a HashSet to store seen elements.

    2. Iterate through the array.

    3. If an element is already in the set, add it to a duplicates set.

    4. Return or print the duplicates.

    Code Example:

    import java.util.*;

    public class DuplicateFinder {
    public static Set<Integer> findDuplicates(int[] array) {
    Set<Integer> seen = new HashSet<>();
    Set<Integer> duplicates = new HashSet<>();
    for (int num : array) {
    if (!seen.add(num)) {
    duplicates.add(num);
    }
    }
    return duplicates;
    }

    public static void main(String[] args) {
    int[] nums = {1, 2, 3, 2, 4, 5, 1};
    System.out.println("Duplicates: " + findDuplicates(nums));
    }
    }
    コピーしました。

    Complexity:

    • Time: O(n)

    • Space: O(n) This is efficient for large datasets and avoids nested loops.

    Using Java Streams

    Steps:

    フィードバック
    ありがとうございました!詳細をお聞かせください
  2. Finding All Duplicates in a List in Java - Baeldung

    2025年3月7日 · In Java, finding duplicate values in an array is a common task often approached with various techniques. One straightforward method involves iterating through the array and comparing …

  3. Java How To Find Duplicate Elements in an Array - W3Schools

    Explanation: We go through the array one element at a time. - The outer loop picks a number (like the first 1). - The inner loop compares it with all the numbers that come after it. - If a match is found, we …

  4. あなたの興味がありそうな検索

  5. Java Program to Find Duplicate Elements in an Array

    Finding duplicate elements in an array is a common problem in programming, especially in data processing tasks. This guide will show you how to create a Java program that identifies and displays …

  6. Java Array, Finding Duplicates - Stack Overflow

    2010年10月17日 · He's expecting duplicates to be false, but every time there are more than 0 elements in the array, the loop will set duplicates to true.

  7. Checking for Duplicates in an Array using Java - Medium

    2024年7月11日 · Checking for duplicates in an array is a common problem that can appear in coding interviews. In this post, we’ll explore different ways to solve the “Contains Duplicate” problem using...

  8. Program to Print the Duplicate Elements of an Array | Find ... - Java

    2025年3月17日 · This algorithm uses two pointers moving at different speeds to detect a cycle in the array, which occurs when duplicates exist under certain constraints (for example, elements act as …

  9. Java - Find the duplicate values of an array of integers - w3resource

    2025年5月9日 · Java exercises and solution: Write a Java program to find duplicate values in an array of integer values.

  10. How To Find Duplicates In Array In Java? - 5 Methods

    2019年1月21日 · In this post, we will learn to find duplicate elements in array in java using Brute Force method, using Sorting method, using HashSet, using HashMap and using Java 8 Streams.

  11. Java Program to Print All the Repeated Numbers with Frequency in an …

    2026年1月9日 · Given an array that may contain duplicate elements, the task is to print only the repeated elements along with their frequencies. If no element is repeated, print NULL.

  12. Finding Duplicate Numbers in Given Array in Java について掘り下げる