bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/Java/Java How To's
Java•Java How To's

Java How To Find Duplicates in an Array

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind Java How To Find Duplicates in an Array?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___[] nums = {1, 2, 3, 2, 4, 5, 1};
3Order

Put the learning moves in the order that makes the concept easiest to apply.

Explanation: We go through the array one element at a time.
Print which elements appear more than once:
Find Duplicate Elements in an Array

Find Duplicate Elements in an Array

Print which elements appear more than once:

Example

int[] nums = {1, 2, 3, 2, 4, 5, 1};
for (int i = 0; i < nums.length; i++) {
  for (int j = i + 1; j < nums.length; j++) {
    if (nums[i] == nums[j]) {
      System.out.println("Duplicate: " + nums[i]);
    }
}
}

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 print it as a duplicate. In this example, the program finds 2 twice and 1 twice, so it prints them as duplicates.

Previous

Java How To - Remove Duplicates from an Array

Next

Java How To - Shuffle an Array