Sum of Unique Elements (LeetCode 1748) | Full solution with a frequency array

  Рет қаралды 1,045

Nikhil Lohia

Nikhil Lohia

Күн бұрын

Join this channel to get access to perks: / @nikoo28
Actual problem on LeetCode: leetcode.com/problems/sum-of-...
Chapters:
00:00 - Intro
00:40 - Problem Statement
02:23 - Brute Force Method
04:01 - Frequency Array
08:21 - Dry-run of Code
10:26 - Final Thoughts
📚 Links to topics I talk about in the video:
Brute Force Approach: • Brute Force algorithms...
Array Data Structure: • Array Data Structure e...
What is Big O?: • Big O Notation Simplif...
Other Easy Problems: • Easy Problems
📘 A text based explanation is available at: studyalgorithms.com
Code on Github: github.com/nikoo28/java-solut...
Test-cases on Github: github.com/nikoo28/java-solut...
📖 Reference Books:
Starting Learn to Code: amzn.to/3sJm8Wl
Favorite book to understand algorithms: amzn.to/4848xJH
Favorite book for data structures: amzn.to/3P96YBv
Get started for interview preparation: amzn.to/44Nn5du
🔗 To see more videos like this, you can show your support on: www.buymeacoffee.com/studyalg...
🎥 My Recording Gear:
Recording Light: amzn.to/3PdsViT
Microphone: amzn.to/3Exv83x
Recording Camera: amzn.to/3PwyN8e
Tablet to sketch and draw: amzn.to/3ZdKVy7
Sketching Tool: amzn.to/45XJEgY
Laptop to edit videos: amzn.to/460ofDu
💻 Get Social 💻
Follow on Facebook at: / studyalgos
Subscribe to RSS feeds: studyalgorithms.com/feed/
Join fan mail: eepurl.com/g9Dadv
#leetcode #programming #interview

Пікірлер: 10
@spandansahu
@spandansahu Ай бұрын
class Solution { public int sumOfUnique(int[] nums) { Map map = new HashMap(); int sum = 0; for (int i = 0; i < nums.length; i++) { if (map.containsKey(nums[i])) { map.put(nums[i], map.get(nums[i]) + 1); } else { map.put(nums[i], 1); } } for (int key : map.keySet()) { if (map.get(key) == 1) { sum += key; } } return sum; } }
@SubhajitDas-mt7sn
@SubhajitDas-mt7sn Ай бұрын
for cpp : class Solution { public: int sumOfUnique(vector& nums) { unordered_mapmp; for(int i:nums) mp[i]++; int sum=0; for(auto it:mp){ if(it.second==1) sum+=it.first; } return sum; } };
@udayrajvadeghar8555
@udayrajvadeghar8555 Ай бұрын
amazing explanation sir!!
@Vikashkumarsinha007
@Vikashkumarsinha007 Ай бұрын
H index please sir
@giftbanda9795
@giftbanda9795 Ай бұрын
I see you have 'int[] freq = new int[101]" what happens when you have an array with 200 elements?
@s260888
@s260888 Ай бұрын
The condition is, it will be less than 101
@nikoo28
@nikoo28 Ай бұрын
the problem constraints limit to 100. If you have 200, then the array will have a length of 200
@giftbanda9795
@giftbanda9795 Ай бұрын
/** * Calculates the sum of unique elements in an array. * Time Complexity: O(n) where n is the length of the array. * Space Complexity: O(n) for storing the elements in the HashMap. */ public static int sumOfUniqueElements(int[] arr) { if (arr.length < 1) return 0; Map map = new HashMap(); int sum = 0; for (int key : arr) { map.put(key, map.getOrDefault(key, 0) + 1); } for (int key : map.keySet()) { if (map.get(key) == 1) { sum += key; } } return sum; }
@code2compass
@code2compass Ай бұрын
In python you can do: return sum(set(arr)) One more thing! Using an array is a good approach but it may not work for all tests because: Arr = [16, 20, 2] For i in range(len(arr)): Arr[i]
@s260888
@s260888 Ай бұрын
@Nikhil Lohia, great work! I’m a big fan of yours. I have a better version of it; please check it out. public int sumOfUnique(int[] nums) { int[] set = new int[101]; int count =0; for(int i=0; i
EVOLUTION OF ICE CREAM 😱 #shorts
00:11
Savage Vlogs
Рет қаралды 13 МЛН
Amazing weight loss transformation !! 😱😱
00:24
Tibo InShape
Рет қаралды 68 МЛН
How to Solve ANY LeetCode Problem (Step-by-Step)
12:37
Codebagel
Рет қаралды 160 М.
The purest coding style, where bugs are near impossible
10:25
Coderized
Рет қаралды 939 М.
Leetcode 46. Permutations : Introduction to backtracking
10:06
ComputerBread
Рет қаралды 91 М.
Max Consecutive Ones (LeetCode 1004) | Full Solution w/ animations
14:41
Kadane's Algorithm | Maximum Subarray Sum | Finding and Printing
20:09
take U forward
Рет қаралды 368 М.
I Solved 1583 Leetcode Questions  Here's What I Learned
20:37
ThePrimeTime
Рет қаралды 601 М.
EVOLUTION OF ICE CREAM 😱 #shorts
00:11
Savage Vlogs
Рет қаралды 13 МЛН