>>> my_set = {1, 2, 3, 4, 5, 1, 2}
>>> my_set
{1, 2, 3, 4, 5}
>>> for i in my_set:
... print(i)
...
1
2
3
4
5
>>> my_set.append(7)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'set' object has no attribute 'append'
>>> my_set[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'set' object does not support indexing
>>> my_set.add(7)
>>> my_set
{1, 2, 3, 4, 5, 7}
>>>
>>> 5 in my_set # look up
True
! hash 는 단방향 암호화이다. 단방향이란 한번 암호화하면 복호화가 안된다는 뜻이다. 실제 값의 길이와 상관없이 hash 값은 일정한 길이를 가진다. 그러므로 hash 는 주로 임의의 길이를 갖는 임의의 데이터에 대해 고정된 길이의 데이터로 매핑할 때 사용된다.
중복된 값을 골라내야 할 때
빠른 look up 을 해야할 때
그러면서 순서는 상관 없을 때
유일한 값을 가지는 배열을 재생성하기
function isDuplicated(arr) {
var mySet = new Set(arr);
return mySet.size < arr.length;
}
console.log(isDuplicated([1,1,2,3]));
console.log(isDuplicated([1,2,3]));
function uniqueElements(list1, list2) {
var mySet = new Set(list1.concat(list2));
return Array.from(mySet);
}
console.log(uniqueElements([1,2,4,5],[ 2,3,5,6]));