Solved Set Mismatch problem in 100 Days of Code

🔥 Day 90 of my 100 Days of Code Problem: Set Mismatch (LeetCode #645) Problem Statement (Simplified): You are given an array representing numbers from 1 to n. One number is duplicated, and another is missing. Return the numbers [duplicate, missing]. Code : class Solution { public int[] findErrorNums(int[] nums) { int n = nums.length; int[] count = new int[n + 1]; int duplicate = -1, missing = -1; // Count occurrences for (int num : nums) { count[num]++; } // Identify duplicate and missing for (int i = 1; i <= n; i++) { if (count[i] == 2) duplicate = i; else if (count[i] == 0) missing = i; } return new int[]{duplicate, missing}; } } Time Complexity: O(n) Space Complexity: O(n) #Day90 #LeetCode #100DaysOfCode #Java #Arrays #Hashing #SetMismatch

  • text

To view or add a comment, sign in

Explore content categories