Counting elements | Leetcode

preview_player
Показать описание
This video explains counting elements problem from leetcode. This video explains 3 different approach to the problem. The first approach is brute-force or naive. The second approach is hybrid of 2 pointer and sliding window technique. The third technique is by using hashmap. This is an important programming interview question. As usual, CODE LINK is given below. If you find any difficulty or have any query then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful...CYA :)

Рекомендации по теме
Комментарии
Автор

While inserting into map, we can just insert by simply doing mymap[element]++. There is no need of checking whether the element is present or not. And in second pass we calculate result.

spetsnaz_
Автор

Sir, my approach is first store the elements in set and next iterate array and check if exist count increement.

kishan
Автор

sir tree and graph ke more programming questions kab upload karoge?

yashgoswami
Автор

Do we really need to use the count in hashmap? Are we actually using the values of hashmap anywhere? Please correct me.

atyab
Автор

Ios base sync stdio false why we use
Explain it brother

retrogame
Автор

Bro, How can I master recursion? I do understand recursion but I can't implement on my own for new problems.

ashokjat
Автор

int a[]={1, 1, 1, 2, 2, 3}, ct=0;

// without hashmap

Arrays.sort(a);
for(int lp=0, rp=1, ps=1;rp<a.length;rp++) {
if (a[lp]==a[rp]) ps++;
if (a[rp]==a[lp]+1) {ct+=ps; lp=rp; ps=1;}
}

// with hashmap
for (int i:a)
if(mp.containsKey(i))
mp.put(i, mp.get(i)+1);
else mp.put(i, 1);
for(int v:mp.keySet())
if(mp.containsKey(v+1))
ct+=mp.get(v);

theoneinyou
Автор

Why are you complicating this much?
class Solution:
def countElements(self, arr: List[int]) -> int:
a, c = [i+1 for i in arr], 0
for i in a:
if i in arr : c+=1
return c

stonecoldcold