Skip to main content

College vs corporate life explained for Indian freshers: compare work, money, time, friends, freedom, stress, and the transition after graduation.

College vs Corporate Life: What Actually Changes After Graduation? One day, you're attending lectures, hanging out with friends, submitting assignments, and worrying about exams. A few months later, you're waking up for work, sitting in meetings, answering messages, dealing with deadlines, and waiting for your salary to arrive at the end of the month. That shift from college to corporate life can feel surprisingly fast. For many Indian students, graduation is not just the end of education. It can mean moving to another city, leaving friends behind, living in a PG or rented room, managing your own expenses, learning how to deal with managers, and figuring out what you actually want from your career. So, what is the real difference between college and corporate life? Is working life harder? Is it better? Do you actually have less freedom? And how long does it take to feel comfortable? The honest answer is that there is no single experience. Your company, role, manager, sa...

217. Contains Duplicate


217. Contains Duplicate

Difficulty: Easy

Problem Statement

Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.


Example 1:

Input:
nums = [1, 2, 3, 1]
Output:
true
Explanation:
The element 1 appears more than once (at indices 0 and 3).


Example 2:

Input:
nums = [1, 2, 3, 4]
Output:
false
Explanation:
All elements are unique.


Example 3:

Input:
nums = [1, 1, 1, 3, 3, 4, 3, 2, 4, 2]
Output:
true
Explanation:
Several elements appear multiple times: 1, 3, 4, and 2.


Constraints:

  • 1 <= nums.length <= 10⁵

  • -10⁹ <= nums[i] <= 10⁹


Solution:



 
import java.util.Arrays;

class Solution {
    public boolean containsDuplicate(int[] nums) {
        Arrays.sort(nums); // Sort the array
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] == nums[i - 1]) return true;
        }
        return false;
    }
}



Sure! Let's go through each approach one by one with a simple explanation:


✅ 1. Using HashSet (Fast and most common)


HashSet<Integer> seen = new HashSet<>();
for (int num : nums) {
    if (seen.contains(num)) return true;
    seen.add(num);
}
return false;

How it works:

  • You keep adding numbers to a HashSet.

  • If you try to add a number that's already in the set → it means it's a duplicate → return true.

Time: O(n)
Space: O(n)


✅ 2. Sorting the array


class Solution {
    public boolean containsDuplicate(int[] nums) {
        Arrays.sort(nums);
        for (int i = 1; i < nums.length; i++) {
if(nums[i] ==nums[i-1])
return true;
        }
        return false;
    }
}
 

How it works:

  • First, sort the array.

  • Then, check if any two adjacent numbers are equal → if yes, duplicate exists.

Time: O(n log n)
Space: O(1) (if sorting in place)


✅ 3. Brute-force (Nested Loops)


for (int i = 0; i < nums.length; i++) {
    for (int j = i + 1; j < nums.length; j++) {
        if (nums[i] == nums[j]) return true;
    }
}
return false;

How it works:

  • Compare each element with every other element.

  • If you find any pair that's equal → return true.

Time: O(n²)
Space: O(1)


✅ 4. Using HashMap



HashMap<Integer, Boolean> map = new HashMap<>();
for (int num : nums) {
    if (map.containsKey(num)) return true;
    map.put(num, true);
}
return false;


How it works:

  • Similar to HashSet, but you're storing each number as a key in a HashMap.

  • If the key is already there → it's a duplicate.

Time: O(n)
Space: O(n)


Summary Table:

Approach Time Complexity Space Complexity Fast? Good for Interviews?
HashSet O(n) O(n) ✅ ✅✅✅
Sorting O(n log n) O(1) ✅ ✅✅
Brute-force O(n²) O(1) ❌ ✅ (for learning only)
HashMap O(n) O(n) ✅ ✅✅


Which one do you want to try implementing?

Comment below.


Comments

Popular posts from this blog

Chocolate Distribution Problem

Chocolate Distribution Problem Given an array  arr[]  of positive integers, where each value represents the number of chocolates in a packet. Each packet can have a variable number of chocolates. There are  m  students, the task is to distribute chocolate packets among  m  students such that -       i. Each student gets  exactly  one packet.      ii. The difference between maximum number of chocolates given to a student and minimum number of chocolates given to a student is minimum and return that minimum possible difference. Examples: Input: arr = [3, 4, 1, 9, 56, 7, 9, 12], m = 5 Output: 6 Explanation: The minimum difference between maximum chocolates and minimum chocolates is 9 - 3 = 6 by choosing following m packets :[3, 4, 9, 7, 9]. Input: arr = [7, 3, 2, 4, 9, 12, 56], m = 3 Output: 2 Explanation: The minimum difference between maximum chocolates and minimum chocolates is 4 - 2 = 2 by choosing following m packe...

33. Search in Rotated Sorted Array

33. Search in Rotated Sorted Array LeetCode 33 – Search in Rotated Sorted Array is a classic binary search problem. The important part is not simply finding the target, but finding it in O(log n) time even though the sorted array has been rotated. The key observation is that, at any point during binary search, at least one half of the current search range will always be sorted . 📄 Problem Statement You are given an integer array nums that was originally sorted in ascending order. All elements are distinct. The array may have been rotated around an unknown pivot. For example: Original: [0,1,2,4,5,6,7] Rotated: [4,5,6,7,0,1,2] You are also given an integer target . Return the index of target if it exists in the array. Otherwise, return -1 . The solution must run in: O(log n) time. 📚 Examples Example 1: Input: nums = [4,5,6,7,0,1,2] target = 0 Output: 4 The target 0 is present at index 4 . Example 2: Input: nums = [4,5,6,7,0,1,2] target = 3 O...