Deduplicate and Sort Math Scores

Introduction

This exercise deduplicates student math scores and prints them high to low—a common data-cleaning pattern. You will use HashSet for uniqueness, ArrayList / Collections for sorting, and skills from Arrays and Streams.

Prerequisites

Project Goal

Given scores (with duplicates):

  1. Remove duplicates
  2. Sort descending
  3. Print clear output

Example:

  • Input: [95, 88, 95, 76, 88, 92, 100, 76]
  • Output: [100, 95, 92, 88, 76]

1) Prepare Sample Data

java
import java.util.ArrayList;
import java.util.List;
 
List<Integer> scores = new ArrayList<>(List.of(95, 88, 95, 76, 88, 92, 100, 76));
 
System.out.println("Original scores:");
System.out.println(scores);

2) Deduplicate with HashSet

java
import java.util.HashSet;
import java.util.Set;
 
Set<Integer> uniqueSet = new HashSet<>(scores);
List<Integer> uniqueList = new ArrayList<>(uniqueSet);

HashSet removes duplicates automatically. Iteration order is not defined—sorting fixes display order.

3) Sort High to Low

java
import java.util.Collections;
 
uniqueList.sort(Collections.reverseOrder());

Alternative:

java
uniqueList.sort((a, b) -> Integer.compare(b, a));

Stream style (Functional Programming):

java
import java.util.stream.Collectors;
 
List<Integer> sorted = scores.stream()
        .distinct()
        .sorted((a, b) -> Integer.compare(b, a))
        .toList();

4) Full Runnable Version

java
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
 
public class DeduplicateAndSortScores {
 
    public static void main(String[] args) {
        List<Integer> scores = new ArrayList<>(List.of(95, 88, 95, 76, 88, 92, 100, 76));
 
        Set<Integer> uniqueSet = new HashSet<>(scores);
        List<Integer> sorted = new ArrayList<>(uniqueSet);
        sorted.sort(Collections.reverseOrder());
 
        System.out.println("Original scores:");
        System.out.println(scores);
        System.out.println("Deduplicated and sorted (high to low):");
        System.out.println(sorted);
    }
}

5) User Input Version

java
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
 
public class DeduplicateAndSortInput {
 
    public static void main(String[] args) {
        List<Integer> scores = new ArrayList<>();
 
        try (Scanner scanner = new Scanner(System.in)) {
            System.out.print("How many scores will you enter? ");
            int count = scanner.nextInt();
 
            for (int i = 1; i <= count; i++) {
                System.out.print("Enter score #" + i + ": ");
                int score = scanner.nextInt();
                if (score < 0 || score > 100) {
                    System.out.println("Invalid score, skipped.");
                    continue;
                }
                scores.add(score);
            }
        }
 
        Set<Integer> unique = new HashSet<>(scores);
        List<Integer> sorted = new ArrayList<>(unique);
        sorted.sort(Collections.reverseOrder());
 
        System.out.println("\n=== Math Score Ranking ===");
        for (int i = 0; i < sorted.size(); i++) {
            System.out.println((i + 1) + ". " + sorted.get(i));
        }
    }
}

6) Enhanced Output with Grade Labels

java
static String gradeFor(int score) {
    if (score >= 90) return "A";
    if (score >= 80) return "B";
    if (score >= 70) return "C";
    return "D";
}
 
// when printing
for (int score : sorted) {
    System.out.println(score + " (" + gradeFor(score) + ")");
}

Surprise Challenge: Grade Statistics

After sorting:

  1. Count how many A, B, C, D grades
  2. Print average of unique scores
  3. Compare with Ranking Game medal idea for top 3 scores

Common Beginner Mistakes

Forgetting Descending Sort

Default sort is ascending—use reverseOrder() or reversed comparator.

Indexing a Set

Convert to List before get(i).

Skipping Validation

Reject scores outside 0–100 when reading input.

What’s Next

Enter advanced topics: Exception Handling.

FAQ

Why HashSet instead of manual duplicate removal?

Clear intent and efficient average-case performance.

Does distinct() in streams preserve order?

distinct() keeps encounter order; HashSet does not—choose tool to match requirement.

Arrays.sort vs Collections.sort?

Arrays.sort for arrays; Collections.sort / list.sort for lists. Ranking Game used Arrays.sort on arrays.

Can duplicates return after sorting?

No—dedup happens before sort in this pipeline.

LinkedHashSet use case?

Preserve first-seen order while removing duplicates, then sort a copy for ranking display.