How to create an array containing non-repeating elements in JavaScript ? Last Updated : 03 Jul, 2024 Comments Improve Suggest changes Like Article Like Report In this article, we will learn how to create an array containing non-repeating elements in JavaScript.The following are the two approaches to generate an array containing n number of non-repeating random numbers.Table of ContentUsing do-while loop and includes() MethodUsing a set and checking its sizeUsing forEach and includes methodMethod 1: Using do-while loop and includes() MethodHere, the includes() function checks if an element is present in the array or not.Example: JavaScript // You can take this value from user const n = 5 // Initial empty array const arr = []; // Null check if (n == 0) { console.log(null) } do { // Generating random number const randomNumber = Math .floor(Math.random() * 100) + 1 // Pushing into the array only // if the array does not contain it if (!arr.includes(randomNumber)) { arr.push(randomNumber); } } while (arr.length < n); // Printing the array elements console.log(arr) Output[ 29, 36, 38, 83, 50 ]Method 2: Using a set and checking its sizeRemember that a set does not allow duplicate elements. Example: JavaScript // You can take this value from user const n = 5 // Initial empty array const arr = []; // Null Check if (n == 0) { console.log(null) } let randomnumbers = new Set, ans; // We keep adding elements till // size of set is equal to n while (randomnumbers.size < n) { // Generating random number // and adding it randomnumbers.add(Math.floor( Math.random() * 100) + 1); } // Copying set elements into // the result array ans = [...randomnumbers]; // Printing the array console.log(ans) Output[ 41, 75, 57, 62, 92 ]Method 3: Using forEach and includes methodUsing forEach and includes to remove duplicates involves iterating over the array and adding each item to a new array only if it isn't already included. This ensures all elements in the result are unique.Example: The removeDuplicates function iterates through an array (`arr`), checking each element. If an element isn't already in nonRepeatingArray, it adds it. This ensures `nonRepeatingArray` contains only unique elements. JavaScript function removeDuplicates(arr) { const nonRepeatingArray = []; arr.forEach(item => { if (!nonRepeatingArray.includes(item)) { nonRepeatingArray.push(item); } }); return nonRepeatingArray; } const arrayWithDuplicates = [1, 2, 3, 4, 2, 3, 5, 1]; const result = removeDuplicates(arrayWithDuplicates); console.log(result); Output[ 1, 2, 3, 4, 5 ] Comment More infoAdvertise with us Next Article How to create an array containing non-repeating elements in JavaScript ? dikshapatro Follow Improve Article Tags : Technical Scripter JavaScript Web Technologies Technical Scripter 2020 javascript-array JavaScript-DSA JavaScript-Questions +3 More Similar Reads How to create a string by joining the elements of an array in JavaScript ? Given an array containing array elements and here we will join all the array elements to make a single string. To join the array elements we use arr.join() method. There are two methods by which we can create a string by joining the elements of an array: Table of Content Using arr.join() MethodUsing 2 min read How to Remove duplicate elements from array in JavaScript ? In this article, we will discuss the methods to remove duplicate elements from a Javascript array. There are various methods to remove duplicates in the array. These are the following ways: Table of Content Using filter() MethodUsing set() MethodUsing forEach() MethodUsing reduce() MethodUsing index 4 min read JavaScript - Create an Array Containing 1...N Numbers Here are some methods to create an array containing 1...N numbers1. Using for LoopThe for loop iterates from 1 to N, adding each number to the array. JavaScriptfunction create(N) { let a = []; for (let i = 1; i <= N; i++) { a.push(i); } return a; } let n = 12; let a = create(n); console.log(a);Ou 3 min read How to add elements to an existing array dynamically in JavaScript ? An array is a JavaScript object that can hold multiple values at a time, irrespective of the data type. In this article, we will see how to add new elements to an existing array dynamically in Javascript. We will discuss two methods in this article i.e. push() method and dynamically adding an elemen 4 min read How to Remove Duplicate Objects from an Array in JavaScript? In JavaScript, it's a common example that the arrays contain objects and there might be a possibility that the objects may or may not be unique. Removing these duplicate objects from the array and getting the unique ones is a common task in Web Development. These are the following approaches: Table 2 min read How to Remove Duplicates in JSON Array JavaScript ? In JavaScript, removing duplicates from a JSON array is important for data consistency and efficient processing. We will explore three different approaches to remove duplicates in JSON array JavaScript. Use the methods below to remove Duplicates in JSON Array JavaScript. Table of Content Using SetUs 3 min read How to generate all combinations of a string in JavaScript ? We are going to see if we can generate all the possible combinations of a given string using JavaScript methods or concepts. You are given a string, containing different characters, you need to generate all combinations of a string by selecting a character at once and then re-arranging that characte 4 min read JavaScript Program to Print All Distinct Elements in an Integer Array 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 ] Outpu 3 min read Count Unique Elements in Array Without Sorting using JavaScript One can count unique elements(distinct elements) present in an array without sorting JavaScript. There are several methods of counting unique elements without sorting in JavaScript. Below is an example to understand the problem clearly. Example:Input: [ 1,2, 3, 1, 3, 4, 5, 5, 2] Output: 5 Explanatio 3 min read How to Create Nested Arrays from a Nest of Arrays in JavaScript ? Creating nested arrays from a nest of arrays in JavaScript involves organizing multiple arrays into a hierarchical structure, which is often useful for managing and representing complex data relationships. This process entails encapsulating arrays within other arrays to form multi-dimensional struct 3 min read Like