Java Script – Set

Array:

  • Array stores duplicate values but same type of elements.
  • For example:
    • marks = [56, 78, 56, 78, 90];

Set:

  • Set doesn’t allow duplicate values.
  • For example:
    • emp_ids = [101, 102, 103, 104]

Map:

  • Map stores Values with Keys.
  • Keys must be unique where as values can be duplicated.
  • For example:
    • accounts = [ [101 : ‘amar’], [102 : ‘annie’], [103 : ‘amar’] ];

Set object methods:

new Set()Creates a new set
add() Adds element to set
delete() removes specified element from set
hash()returns true if the value exists
clear()remove all elements
forEach()invokes each element
values() returns an iterator with all values
keys() same as values()
entries() returns [key, value] pairs in Map but [value, value] pairs in Set

Create new set and add elements:

<!doctype html>
<html>
	<body>
		<script>
			var colors = new Set();

			colors.add("black");
			colors.add("blue");
			colors.add("green");
			colors.add("cyan");
			colors.add("red");

			document.write(colors);
		</script>
	</body>
</html>

Output :  [Object Set]

We can display values of any collection using for-each loop:

<html>
	<body>
		<script>
			var colors = new Set();
			colors.add("black");
			colors.add("blue");
			colors.add("green");
			colors.add("cyan");
			document.write("Colors are : <br/>");
			for(var col of colors){
				document.write(col + "<br/>");
			}
		</script>
	</body>
</html>

Construct Set from the Array directly:

<html>
	<body>
		<script>
			var arr = ["black", "blue", "green", "cyan", "red"];
			var colors = new Set(arr);
			document.write("Colors are : <br/>");
			for(var col of colors){
				document.write(col + "<br/>");
			}
		</script>
	</body>
</html>

Note: If we create Set from Array – all duplicates will be removed automatically.

<html>
	<body>
		<script>
			var arr = ["a", "e", "i", "o", "u", "i", "e", "u", "a", "i"];
			var vowels = new Set(arr);
			document.write("Vowels are : <br/>");
			for(var v of vowels){
				document.write(v + "<br/>");
			}
		</script>
	</body>
</html>

Collect value from set and pass to the function to perform any operation in function:

<html>
	<body>
		<script>
			var nums = new Set([7, 4, 2, 9, 3, 6, 1, 8]);
			for(var n of nums){
				even(n);
			}
			function even(x){
				if(x%2==0)
					document.write(x + "<br/>");
			}
		</script>
	</body>
</html>

forEach() method: invokes function for each set element

<html>
	<body>
		<script>
			var nums = new Set([7, 4, 2, 9, 3, 6, 1, 8]);
			nums.forEach(function(n){
				if(n%2==0){
					document.write(n);
				}
			})
		</script>
	</body>
</html>

forEach() with lambda expression:

<!doctype html>
<html>
	<body>
		<script>
			var nums = new Set([7, 4, 2, 9, 3, 6, 1, 8]);
			nums.forEach(n => {document.write(n);})
		</script>
	</body>
</html>

Sum of elements in Set using forEach() method and lambda expression:

<!doctype html>
<html>
	<body>
		<script>
			var nums = new Set([7, 4, 2, 9, 3, 6, 1, 8]);
			var sum=0;
			nums.forEach(n => sum+=n)
			document.write("Sum : " + sum);
		</script>
	</body>
</html>

values(): this method returns an iterator with set elements:

<!doctype html>
<html>
	<body>
		<script>
			var nums = new Set([7, 4, 2, 9, 3, 6, 1, 8]);
			var itr = nums.values();
			document.write(itr);
		</script>
	</body>
</html>

Iterate elements using for/of loop:

<!doctype html>
<html>
	<body>
		<script>
			var nums = new Set([7, 4, 2, 9, 3, 6, 1, 8]);
			var itr = nums.values();
			
			for(var x of itr)
			{
				document.write(x);
			}
		</script>
	</body>
</html>

Iterate elements using next() method:

<!doctype html>
<html>
	<body>
		<script>
			var nums = new Set([7, 4, 2, 9, 3, 6, 1, 8]);
			var itr = nums.values();
			
			var x = itr.next();
			while(!x.done)
			{
				document.write(x.value);
				x = itr.next();
			}
		</script>
	</body>
</html>
Scroll to Top