50DaysOfStreams – Day 4 ✅ Day 4: Find the First Non-Repeated Character in a String 🧩 Problem: Given a string, find the first non-repeating character using Java Streams. Solution: String input = "swiss"; Optional<Character> result = input.chars() .mapToObj(c -> (char) c) .collect(Collectors.groupingBy( Function.identity(), LinkedHashMap::new, Collectors.counting() )) .entrySet() .stream() .filter(entry -> entry.getValue() == 1) .map(Map.Entry::getKey) .findFirst(); result.ifPresent(System.out::println); Output: w 🛠 Concepts Used: chars() mapToObj() groupingBy() LinkedHashMap (to maintain order) findFirst() See you tomorrow for Day 5 🔥 #Java #Streams #FunctionalProgramming #CodingChallenge #BackendDevelopment #LearningInPublic
Java Streams: Find First Non-Repeating Character in String
More Relevant Posts
-
📅 Day 3/60 – #60DaysOfCode Challenge 🚀 🔹 Problem: Union of Two Arrays Given two arrays a[] and b[], find the number of distinct elements present in the union of both arrays. 🧠 Key Concept: ➡️ Use a HashSet to store only unique elements ➡️ Add elements from both arrays into the set ➡️ The size of the set gives the union count 💻 Java Solution (Optimal Approach): import java.util.*; class Solution { public static int findUnion(int a[], int b[]) { HashSet<Integer> set = new HashSet<>(); // Traverse first array and add each element to the set // HashSet automatically ignores duplicate values for(int num : a) { set.add(num); } // Traverse second array and add each element to the set // Only distinct elements will remain in the set for(int num : b) { set.add(num); } // The size of the set represents the count of unique elements // in the union of both arrays return set.size(); } } ✨ This problem helped me revise: ✔️ HashSet usage ✔️ Union of arrays concept ✔️ Optimizing from brute-force to O(n + m) #Day3 #CodingChallenge #Java #DataStructures #HashSet #ProblemSolving #TechJourney
To view or add a comment, sign in
-
🚀 Day 76 of #100DaysOfCode Today I solved a string manipulation problem: Clear Digits 🧠 Problem: Given a string, whenever a digit appears, remove the previous character. Digits act like a backspace operation. 💡 Key Insight: Instead of modifying the string directly (since Strings are immutable in Java), I used a StringBuilder, which works like a stack: If character → append (push) If digit → delete last character (pop) 📌 What I Learned: StringBuilder is powerful for string modifications Always handle edge cases (like digit at the start) Many string problems are actually stack problems in disguise ⏱ Complexity: Time: O(n) Space: O(n) #Java #DSA #LeetCode #CodingJourney #100DaysOfCode #ProblemSolving
To view or add a comment, sign in
-
-
Day 45 — LeetCode Progress (Java) Problem: Intersection of Two Arrays Required: Given two integer arrays nums1 and nums2, return an array containing their unique intersection elements. Idea: Use a HashSet to efficiently track elements from the first array and check their presence in the second array. Approach: Create a HashSet to store all elements from nums1. Traverse nums1 and insert each element into the set. Initialize a list to store the intersection result. Iterate through nums2: If an element exists in the set: Add it to the result list. Remove it from the set to avoid duplicates. Convert the result list into an array and return it. Time Complexity: O(n + m) n = length of nums1 m = length of nums2 Space Complexity: O(n) #LeetCode #DSA #Java #HashSet #Arrays #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
Day 14/100 – LeetCode Challenge 🚀 Problem: #169 Majority Element Difficulty: Easy Language: Java Approach: Sorting + Middle Element Time Complexity: O(n log n) Space Complexity: O(1) 🔍 Key Insight: The majority element appears **more than ⌊n / 2⌋ times** in the array. If we **sort the array**, the majority element must occupy the **middle position** because it appears more than half of the time. Therefore, the element at index **n/2** will always be the majority element. 🧠 Solution Brief: First sorted the array using `Arrays.sort()`. Since the majority element appears more than half of the array length, it will always be positioned at the middle index. Finally returned `nums[nums.length / 2]` as the majority element. 📌 What I Learned: Understanding problem constraints can simplify the solution significantly. Sometimes a simple observation (like majority occupying the middle after sorting) can avoid more complex implementations. #LeetCode #Day14 #100DaysOfCode #Java #DSA #Arrays #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
#Day_53_of_my_DSA_Journey (mission: abki bar string, array par) **Array to Linked List in Java! Today converted an array into a singly linked list using Java. 🖥️ ✅ Created a Node class with constructors for flexibility ✅ Built a method arrToLink to dynamically create a linked list from any array ✅ Practiced fundamental data structure concepts ->Start with the first element ->Create the head node using the first element of the array. ->Initialize a pointer called mover to track the last node in the list. ->Loop through the rest of the array ->For each element, create a temporary node (temp) containing the current value. ->Link it to the current last node (mover.next = temp). ->Move the mover pointer forward (mover = temp) so it always points to the last node. ->Repeat until all array elements are linked. ->Return the head — now pointing to a complete linked list! **Why this works: ✅mover keeps track of where the next node should be attached. ✅temp is a fresh node for each array element. ✅Together, they efficiently build the linked list in one pass. 💡 Complexity: TC: O(n) — single pass through the array SC: O(n) — new nodes created for the linked list 📌 Linked lists are essential for understanding dynamic memory allocation and pointer manipulation. #Java #DataStructures #LinkedList #CodingPractice #DSA #Programming #TechLearning
To view or add a comment, sign in
-
-
Day 6/100 – LeetCode Challenge 🚀 Problem: #189 Rotate Array Difficulty: Medium Language: Java Approach: Array Reversal Technique Time Complexity: O(n) Space Complexity: O(1) 🔍 Key Insight: Instead of shifting elements one by one, the array can be rotated efficiently using a three-step reversal strategy. Steps: 1️⃣ Reverse the entire array 2️⃣ Reverse the first k elements 3️⃣ Reverse the remaining elements This achieves the required rotation in-place with constant extra space. 🧠 Solution Brief: Calculated k % n to handle cases where k is greater than array length. Reversed the entire array first. Then reversed the first k elements and the remaining n-k elements. This sequence correctly rotates the array to the right. 📌 What I Learned: Understanding patterns like array reversal can simplify problems that initially seem complex. Optimizing from brute force shifting to an in-place O(n) solution improves efficiency. #LeetCode #Day6 #100DaysOfCode #Java #DSA #Arrays #RotateArray #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
Day 14- What I Learned in a Day(JAVA) Concatenation Operator :("+") • Used to combine two or more strings • Commonly used to join text with variables • Helps in displaying meaningful output messages Increment Operator: • Used to increase a variable value by one • Can be used in pre-increment(++varname) and post-increment(varname ++) form Decrement Operator: • Used to decrease a variable value by one • Can be used in pre-decrement(--varname) and post-decrement(varname--) form. 🔹 I practiced small programs to understand how these operators behave during execution and how output changes based on their usage. Practiced 👇 #Java #JavaProgramming #OperatorsInJava #CodingPractice #LearningJourney #ProgrammingLife #TechSkills #FutureDeveloper 🚀
To view or add a comment, sign in
-
🚀 Day 60 — HashMap & Frequency Counting 🧩 3659. Partition Array Into K-Distinct Groups Today I solved a problem using HashMap and frequency counting in Java. 🧠 Approach: 🔹 First, count the frequency of each number using a HashMap. 🔹 Since every group must contain k distinct elements, the maximum number of groups possible is: groups=nums.lengthk\text{groups} = \frac{nums.length}{k}groups=knums.length🔹 If any element appears more than the number of groups, it cannot be distributed without repeating in a group. 🔹 Also check if the array length is divisible by k. 💡 Key Insight: If frequency of any element > total groups, forming valid groups with distinct elements becomes impossible. ⏱️ Complexity: Time: O(n) Space: O(n) #Day60 #LeetCode #Java #HashMap #DSA #ProblemSolving #Consistency 💪🚀
To view or add a comment, sign in
-
-
Today I solved LeetCode 131 – Palindrome Partitioning. 🧩 Problem Summary: Given a string s, partition the string such that every substring in the partition is a palindrome. Return all possible palindrome partitioning combinations. 💡 Key Concepts Used: Backtracking Recursion Depth First Search (DFS) String manipulation Palindrome checking 🧠 Approach: Use backtracking to explore all possible partitions of the string. Start from index 0 and try every possible substring. Check whether the substring is a palindrome. If it is a palindrome, add it to the current partition path. Recursively continue exploring the remaining substring. Backtrack to explore other possible partitions. 📚 What I Learned: How to divide a problem into smaller recursive partitions. Efficiently checking palindrome substrings. Using backtracking to explore all valid partition combinations. Managing recursive states and undoing choices properly. Every problem solved strengthens problem-solving skills 💡 Consistency and practice make the difference 🚀 #LeetCode #DSA #Backtracking #Recursion #PalindromePartitioning #CodingJourney #Java #ProblemSolving #100DaysOfCode #TechGrowth
To view or add a comment, sign in
-
-
𝗦𝘁𝗿𝗶𝗻𝗴𝗕𝘂𝗶𝗹𝗱𝗲𝗿 𝘃𝘀 𝗦𝘁𝗿𝗶𝗻𝗴𝗕𝘂𝗳𝗳𝗲𝗿 𝗶𝗻 𝗝𝗮𝘃𝗮 — 𝗪𝗵𝗮𝘁’𝘀 𝘁𝗵𝗲 𝗗𝗶𝗳𝗳𝗲𝗿𝗲𝗻𝗰𝗲? When working with strings in Java, performance and thread safety matter. That’s where StringBuilder and StringBuffer come in. ✅ StringBuilder - Faster performance - Not thread-safe - Best for single-threaded environments ✅ StringBuffer - Thread-safe (synchronized) - Slightly slower due to synchronization - Best for multi-threaded applications 💡 Both are mutable, unlike String — which means they can modify content without creating new objects, making them memory-efficient for heavy string operations. 👉 Use StringBuilder for speed, and StringBuffer when thread safety is required. #Java #StringBuilder #StringBuffer #JavaProgramming #CoreJava #ProgrammingConcepts #JavaInterview #DeveloperTips #Coding #SoftwareDevelopment
To view or add a comment, sign in
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development