Solved LeetCode #2442 with HashSet and Number Reversal in Java

🚀 Just solved LeetCode #2442 — Count Number of Distinct Integers After Reverse Operations 📘 Problem: You are given an array of positive integers. For each integer, reverse its digits and add it to the array. Finally, return the total count of distinct integers in the resulting array. Example: Input → [1,13,10,12,31] Output → 6 Explanation → After reversing and adding, the array becomes [1,13,10,12,31,1,31,1,21,13]. The distinct numbers are 1, 10, 12, 13, 21, and 31. 🧠 My Approach: I used a HashSet to automatically handle uniqueness. 1️⃣ Loop through each element in the array and add it to the set. 2️⃣ Reverse each number and add the reversed version as well. 3️⃣ Return the final size of the HashSet, which represents the count of distinct integers. 💡 What I Learned: ✅ How HashSet simplifies handling of unique elements ✅ Efficiently reversing numbers using arithmetic instead of strings ✅ Reinforced understanding of loops and data structures working together 💻 Language Used: Java 🔍 Concept: HashSet | Array Manipulation | Number Reversal #LeetCode #Java #DSA #HashSet #CodingUpdate #LearningByDoing #Programming

  • graphical user interface, text, application, email

To view or add a comment, sign in

Explore content categories