JavaScript Program to Print All Distinct Elements in an Integer Array
Last Updated :
04 Jun, 2024
Given an Array of Integers consisting of n elements with repeated elements, the task is to print all the distinct elements of the array using JavaScript. We can get the distinct elements by creating a set from the integer array.
Examples:
Input : arr = [ 1, 2, 3, 4, 4, 5, 5, 6, 7, 7, 8, 9, 9 ]
Output : [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
Explanation: The input array consist of 4 repeating integers
and they are: 4, 5, 7, and 9. After removal of the duplicates
elements final array will be: [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
Input : arr = [ 4, 4, 4, 4, 4 ]
Output : [ 4 ]
Methods to Print All Distinct Elements of a Given Array of Integer
- Using Set Constructor with Spread Operator
- Using Array forEach() and include() Methods
JavaScript Program to Print Distinct Elements using Set Constructor with Spread Operator
An easy solution for the above problem is to create a set object from the elements of the array as a set contains only distinct elements. Then use the Spread Operator ( ... ) on the set to form a new array and then print the array.
Example: In this example, we will create new set object from arrray by iterating using spread operator.
JavaScript
// Given array
const arr = [1, 2, 3, 4, 4, 5, 5, 6, 7, 7, 8, 9, 9];
// Creating new array with Distinct elements
const distinctArr = [...new Set(arr)];
// Display output
console.log(distinctArr);
Output[
1, 2, 3, 4, 5,
6, 7, 8, 9
]
JavaScript Program to Print Distinct Elements using Array forEach() and include() Methods
We can also solve this problem by using forEach() loop on the input array and include() method to check distinct values.
- Create an empty array.
- Iterate over the array using forEach loop and specify a parameter 'num' .
- Check if the distinct array doesn't contain the current element of the input array (num), only then push the current element on the distinct array.
- Print the distinct array.
Example: In this example, we will use array.forEach and include methods to get distinct array.
JavaScript
// Create an array with duplicate elements
const arr = [1, 2, 3, 4, 4, 5, 5, 6, 7, 7, 8, 9, 9];
// Create an empty array
const distinctArr = [];
// Using forEach() and includes() method
// to get the unique elements
arr.forEach((num) => {
if (!distinctArr.includes(num)) {
distinctArr.push(num);
}
});
console.log(distinctArr);
Output[
1, 2, 3, 4, 5,
6, 7, 8, 9
]
Method: Using Object Property as Flags
In this approach, we use an object to keep track of whether we have encountered an element or not. We iterate through the array, and for each element, we check if it exists as a property in the object. If it doesn't, we add it to the object and push it into the distinct array.
JavaScript
function printDistinctElements(arr) {
const distinctArr = [];
const encountered = {};
for (let i = 0; i < arr.length; i++) {
if (!encountered[arr[i]]) {
encountered[arr[i]] = true;
distinctArr.push(arr[i]);
}
}
return distinctArr;
}
const arr = [1, 2, 3, 4, 4, 5, 5, 6, 7, 7, 8, 9, 9];
const distinctArr = printDistinctElements(arr);
console.log(distinctArr);
Output[
1, 2, 3, 4, 5,
6, 7, 8, 9
]
Similar Reads
Print all Distinct (Unique) Elements in given Array Given an integer array arr[], print all distinct elements from this array. The given array may contain duplicates and the output should contain every element only once.Examples: Input: arr[] = {12, 10, 9, 45, 2, 10, 10, 45}Output: {12, 10, 9, 45, 2}Input: arr[] = {1, 2, 3, 4, 5}Output: {1, 2, 3, 4,
11 min read
Making elements distinct in a sorted array by minimum increments Given a sorted integer array. We need to make array elements distinct by increasing values and keeping array sum minimum possible. We need to print the minimum possible sum as output. Examples: Input : arr[] = { 2, 2, 3, 5, 6 } ; Output : 20 Explanation : We make the array as {2, 3, 4, 5, 6}. Sum be
10 min read
Find sum of non-repeating (distinct) elements in an array Given an integer array with repeated elements, the task is to find the sum of all distinct elements in the array.Examples: Input : arr[] = {12, 10, 9, 45, 2, 10, 10, 45,10};Output : 78Here we take 12, 10, 9, 45, 2 for sumbecause it's distinct elements Input : arr[] = {1, 10, 9, 4, 2, 10, 10, 45 , 4}
14 min read
Product of non-repeating (distinct) elements in an Array Given an integer array with duplicate elements. The task is to find the product of all distinct elements in the given array. Examples: Input : arr[] = {12, 10, 9, 45, 2, 10, 10, 45, 10}; Output : 97200 Here we take 12, 10, 9, 45, 2 for product because these are the only distinct elements Input : arr
15 min read
Print sorted distinct elements of array Given an array that might contain duplicates, print all distinct elements in sorted order. Examples: Input : 1, 3, 2, 2, 1Output : 1 2 3Input : 1, 1, 1, 2, 2, 3Output : 1 2 3The simple Solution is to sort the array first, then traverse the array and print only first occurrences of elements. Algorith
6 min read
JavaScript Program to Find Element that Appears once in Array where Other Elements Appears Twice Given an Array of Integers consisting of n elements where each element appears twice except one element. The task is to find the element which appears only once in the input array in JavaScript. Example: Input : arr = [ 5, 9, 2, 7, 4, 8, 6, 1, 5, 1, 4, 6, 3, 7, 8, 2, 9 ] Output : 3Explanation: The i
3 min read
Construct an Array having K Subarrays with all distinct elements Given integers N and K, the task is to construct an array arr[] of size N using numbers in the range [1, N] such that it has K sub-arrays whose all the elements are distinct. Note: If there are multiple possible answers return any of them. Examples: Input: N = 5, K = 8Output: {1, 2, 3, 3, 3}Explanat
7 min read
Find an array element such that all elements are divisible by it Given an array of numbers, find the number among them such that all numbers are divisible by it. If not possible print -1. Examples: Input : arr = {25, 20, 5, 10, 100} Output : 5 Explanation : 5 is an array element which divides all numbers. Input : arr = {9, 3, 6, 2, 15} Output : -1 Explanation : N
8 min read
k-th distinct (or non-repeating) element among unique elements in an array. Given an integer array arr[], print kth distinct element in this array. The given array may contain duplicates and the output should print the k-th element among all unique elements. If k is more than the number of distinct elements, print -1.Examples:Input: arr[] = {1, 2, 1, 3, 4, 2}, k = 2Output:
7 min read
Queries for number of distinct elements from a given index till last index in an array Given a array âa[]â of size n and number of queries q. Each query can be represented by a integer m. Your task is to print the number of distinct integers from index m to n i.e till last element of the array. Examples: Input: arr[] = {1, 2, 3, 1, 2, 3, 4, 5}, q[] = {1, 4, 6, 8} Output: 5 5 3 1 In qu
6 min read