0% found this document useful (0 votes)
10 views10 pages

Hash Set Notes

Uploaded by

Bharath Karapuri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views10 pages

Hash Set Notes

Uploaded by

Bharath Karapuri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Hask Set:

, 2, 3, 3,3, 3, 4,5, 5,6, 4


* IH oll not al
, ,9,4 3

eleme kt t
5, ,

Hash Sete Datay pe Set = neus HashSete);


Setadd1)
set add (2)
2|
Set' add (3)
3
St add (3)
Set add (4)
The addc) method intenally call, the pute method
map put (e, " PRE SENT);"
Before adding the elements the add methods cheek it its available in the
map
boolean, add CTateger e ) :
vtixm mapputle, PRESENT) r=null,
map put (1, "PRESENT) =utl -’ True
hap p u t , ) nelTue
map put e,
u null false
be Second time lor) henever he same element is added So the set

Insertion Delstion
'ou) oti)
* Too Sum:
[1,234 5,6,] "Target s
ole: (, 4),(,3)
Brut Force: forl int izo, i <arr. lengh; l++)!
o)
S.0 pl (ar ei] ar J)
for (Pot izo, i<o, it){
nt num z arr C
int res target -num,
f(set: contaios (es) oln) oln)

Set add (num )

## Contains Dupleato:
Ie: ,i,, 2,3, 2,2,4,4,s pTeaé"
add elementa, in the set bg tebtaating the
cheek if the elemeto is aheady avacle in the set.
TL avalable vetum True.
for Cint ie o, i<ar. lengh, et)
i(set- containa (areti)] £
retum rue,

return false:
# ## Jesels and Stores:
Jewel: "aa, stoner"aAAb66b
’ checkeach jeel in the stone
Brute Fora: heating thrng Jeolg ones andchek cash chaxache l
for (înt ie o, iz stones. lengthyit) found countt,

f Ciesol- char Atli)== Stones-charAt (i) )!

retum Count;
We Can ude Lndes of Appreeth
t. indexof(a) ’ o
t aA
t indexof (^) - |
for (iot ieo, ie store s. leng thuy, f)
char eh stone s. char Atei);
if( jevels. iodexof (eh)! -){
Counttt}

retum count
Uing kanh Seticor) D-A:T
Creat an Array diae ars.

-’ Tterat the tornes, char fuom stones is awailable in the aay


by checking the valaa is 1.
ye inerement Cqunt

frqung [uoals charAt]t=l;


iot Count =0
for (int j=oy i<stores lenthesçit)f
ifl freaueng [stonet-charfALD] =1)
Count tt')

return ount
##eppy Number
5+ happy hannbex
a)
Sating
) Repeat the precens uctil ie nmben ceyela i
) end witth, is kappy ulee
2 2=4

20
Algest:
-Add the elemen+ io the hashset
Mumee t the
2 Compute the Am 4 saqyuanes and as u sompcte, add hat
3- 3f conmputed nember is l etrn true
sL computod mmnbee akeady preuent in 'the set -’ rehutn flse;
while (truej' t h

whileln!=o)<
Sum t= Math pow(n210, 2 - 0 ) ; +

¡ftsum==) return true;


n 82

if(set Contains (n)) !


retur false;
}
Set. ad d(;
-Maxtmum Windes ubdtrig
I|: S: "ADDBE CO DE BANColr: BANC
t: "ABC

A
'Bi eired meey
fouud. aleeedy
’when al thekys. in the map have become 2os, wehave seahed
one potential a
In the above ppnoach, we need to iterote tthrongh the' mapto cheek
all the Volues
de we wiu have a new
proach with count vaiable
fnitialtae count = haskmap. sizeey
when any value Beoms Zre, eduee -the Count
lount be lomes ' e
Ohen
seask the potential windo.
S: TrttA + TA
To*- -3
A
Se potential windouw 5
ogot one potential window

windw
So
potentinl windao' e

2
pointee’ Windovw tant and Windo¡End.
Dumpthe elemert String
haskmap tale wcth he ocerence
3. oeot anethe vauiable
Called Count
4 yAtat
5 the tsuaveaingat iwingbeation i
element
, Deerement the present inthe map
valt of that kay . keay
b) T
i)
Count =0
shrink he oindo and cheek
beconne ze, Count-
thax at aoiaking we ane
qctit
G) tasct tuakouing ni ocatiom and check ethlue that
Pasticalas elmeut in yowe hahap
(hi) sink he winda iiL te Cout =0,

Map <Charactn,Tntagers tarqetMap - neuo Hshiapes;


for ( char c: b to ChaR Arrayt) f
target Map- Putle, targ ct Map. getor Defautt (e, o) +);
int i-o, j: o;
int count = targetMop- slzec) ;
int minlen = Tnteqet.
indntartMia
int Pet =o
MM-VALD E)

char jehar =s-charAt G);


iF (targetMep.containskey ichar))
tatgetMap- putlJchat, targ&Nap get ihar
if(targ etMap. get Cjchar) =zo)
Count --

while tount == o)
iti-it t< ninlen)
minlen -i -iti,
toin dow tart Min =i:

eha ichas S-ehae At(i


if (tasget Map. Contains key ih))

f (fatget Map- qetli chan) )i s


Count tt,

rewmminlen Inteqer--VALVE ? : S. sabshingtiaGartli


Oindeut
Brle Feei
’Ithrate through-he axeay and check if he nent eloment is avalae

Oplimiged -pproacl..
o ’ checking ate the elemesti thrgh

OO, 4, 200, I, 3, º
’hdd al elementt to theHash et
check it he previous num s aveilaßle
wthe hah t (becnuAe the pre ui
’Il te previous - incremeot the number
and check in the set, cont ains inere ment the nuwher
and length
’ Get the Max and max
L00,, 3,
7 Ato:
At : 4

2: 200 g - ) I39 numt 2004|= 201 (*)


At 3: ’ (n-) OXnum t 2
numt; ’2 length 2
nt1)3/
nu,m t t ' ) R leugth
Num ttj.

(o ) wax leugth 4
lengh man retarn
tength, max( Matk:monlengthe
tjlengtht
containirum
t1)) (nun (set. kile
Corntains (set. it
length sot
num nt
[iD; lar ad4 set
Sbesc new set=
Hath Set<Intcqers
length
eo mar int
ength
9 ma
Co,
5: At
C, o, -, o, -2, 2]
dot
-2+ (-) +o+2 ’Surm -o
|-2, -, o, o, I, 2
left righkt
left +t and array cleft] =array[lett-i]:
ta again lefttfi toi)
[-2, -, , o,
lett ight -Add t the

Next increment j as lefttright iir a),Ra

right sintt)) :
Add to tpe list
C-2,o, o, J kftt, rigt--;
(-2,-, o, o,, 2] tett* iaht
tvight
So, increme nti
-1+0+ o+2 o

right - b hkril
[-2, -, o, o,,2.2 +ot
add to the lit
C-,o,o,)
So final Recalt Contains
[-a,,] E,9,0,2] [-,9,o,)
int n: amag. length;
lonq Surm
Su O
Set<ist <Inteqer>> set s new tash Set<>);
Arays sort (array);
for Cint is O i< n- , it+)
ift isö & amag ti) =s array lt-13 ) cotnue;
for l int je i, j<o-2it)f
it (i>& aray ti]=z array ti-)continue;
int lett = t
int right a n-l;
Sum= (long) arraylil aray Li] taray Cla) t aray igle]
if (Sum >target)

else if (sum <target)


r

clue
List enteger , isteneo Anybites t;
Iistadd array til);
list-add Carray Csl);
istadd Carray ClePtl);
1 tist add
CarrayGigh] ;
Set add Cist);

(
arayc luft-))

ray brglt
retum new
-Araylit <> (set);

You might also like