Pages

Sunday, April 27, 2025

Set mismatch in C++ (Assignment) incld

 class Solution {

public:
    vector<int> findErrorNums(vector<int>& nums) {
        int n = nums.size(); // size of array

        vector<int> count(n+1, 0); // to store frequency

        for(int i=0; i<n; i++) { // traverse array
            count[nums[i]]++; // increase count
        }

        int duplicate = 0, missing = 0;

        for(int i=1; i<=n; i++) { // from 1 to n
            if(count[i] == 2) duplicate = i;
            if(count[i] == 0) missing = i;
        }

        return {duplicate, missing};
    }
};



I faced difficulties while working on.. 1. finding the size of array:- nums.size(); is the correct syntax.
2. to store frequency of arrays:- vector<int> count(n+1, 0).
3. Traverse Array:- (int i=0; i<n; i++) // traverses array
                       count[nums[i]]++; // increases the count of traverse

4. in Leetcode or Coding interviews, always:- return {duplicate, missing};

No comments:

Post a Comment

Stack using Linked List – Partial (University Exam Topic)

 #include <iostream> using namespace std; struct Node {     int data;     Node* next; }; class Stack {     Node* top; public:     Stac...