Object Oriented Programming Articles

Page 4 of 589

How do I make an array with unique elements (remove duplicates) - JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 271 Views

In JavaScript, there are several ways to remove duplicate elements from an array and create a new array with unique values only. Using filter() with indexOf() The filter() method combined with indexOf() is a traditional approach that keeps only the first occurrence of each element: var duplicateNumbers = [10, 20, 100, 40, 20, 10, 100, 1000]; console.log("Original array:"); console.log(duplicateNumbers); var uniqueNumbers = duplicateNumbers.filter(function (value, index, array) { return array.indexOf(value) === index; }); console.log("Array with unique elements:"); console.log(uniqueNumbers); Original array: [ 10, 20, 100, ...

Read More

How to replace some preceding characters with a constant 4 asterisks and display the last 3 as well - JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 951 Views

In JavaScript, you can mask sensitive data by replacing preceding characters with asterisks while keeping the last few characters visible. This is commonly used for phone numbers, credit cards, or account numbers. Problem Statement Given these input values: '6778922' '76633 56 1443' '8888 4532 3232 9999' We want to replace all preceding characters with 4 asterisks and display only the last 3 characters: **** 922 **** 443 **** 999 Using replace() with Regular Expression The replace() method with a regex pattern can capture the last 3 characters and replace ...

Read More

Splitting a hyphen delimited string with negative numbers or range of numbers - JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 521 Views

When working with hyphen-delimited strings containing negative numbers or ranges, simple split('-') won't work correctly because it treats every hyphen as a delimiter. We need regular expressions to distinguish between separating hyphens and negative number signs. The Problem Consider these strings with mixed content: var firstValue = "John-Smith-90-100-US"; var secondValue = "David-Miller--120-AUS"; Using split('-') would incorrectly split negative numbers and ranges, giving unwanted empty strings and broken values. Solution Using Regular Expression We use a lookahead pattern to split only at hyphens that precede letters or number ranges: var firstValue ...

Read More

Compare the Triplets - JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 234 Views

The "Compare the Triplets" problem is a popular algorithmic challenge where you compare two arrays of three integers each and count how many comparisons each array wins. Alice and Bob each have three scores, and we need to determine who wins more individual comparisons. Problem Statement Given two triplets (arrays of 3 elements each), compare corresponding elements and award points: 1 point to Alice if her element is greater, 1 point to Bob if his element is greater, 0 points for ties. Return an array with [Alice's score, Bob's score]. Example Input Alice: [5, 6, ...

Read More

How to redundantly remove duplicate elements within an array – JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 331 Views

Let's say we have an array with duplicate elements like this: [10, 20, 10, 50, 60, 10, 20, 40, 50] JavaScript provides several methods to remove duplicate elements from arrays. The most common and efficient approach is using the Set object with the spread operator. Using Set with Spread Operator (Recommended) The Set object stores only unique values. Combined with the spread operator, it creates a new array without duplicates: var originalArray = [10, 20, 10, 50, 60, 10, 20, 40, 50]; var arrayWithNoDuplicates = [...new Set(originalArray)]; console.log("Original array:", originalArray); console.log("No ...

Read More

How to replace before first forward slash - JavaScript?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 877 Views

Let's say the following is our string with forward slash — var queryStringValue = "welcome/name/john/age/32" To replace before first forward slash, use replace() along with regular expressions. Syntax string.replace(/^[^/]+/, "replacement") Example Following is the code — var regularExpression = /^[^/]+/ var queryStringValue = "welcome/name/john/age/32" var replacedValue = queryStringValue.replace(regularExpression, 'index'); console.log("Original value=" + queryStringValue); console.log("After replacing the value=" + replacedValue); Original value=welcome/name/john/age/32 After replacing the value=index/name/john/age/32 How the Regular Expression Works The regular expression /^[^/]+/ breaks down as follows: ^ ...

Read More

Convert number to a reversed array of digits in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 238 Views

Given a non-negative integer, we need to write a function that returns an array containing the digits in reverse order. This is a common programming challenge that demonstrates string manipulation and array operations. Example 348597 => The correct solution should be [7, 9, 5, 8, 4, 3] Method 1: Using String Split and Reverse The most straightforward approach is to convert the number to a string, split it into individual characters, reverse the array, and convert back to numbers: const num = 348597; const reverseArrify = num => { ...

Read More

JavaScript Get English count number

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 258 Views

We are required to write a JavaScript function that takes in a number and returns an English ordinal number for it (1st, 2nd, 3rd, 4th, etc.). Example 3 returns 3rd 21 returns 21st 102 returns 102nd How Ordinal Numbers Work English ordinal numbers follow these rules: Numbers ending in 1: add "st" (1st, 21st, 31st) — except 11th Numbers ending in 2: add "nd" (2nd, 22nd, 32nd) — except 12th Numbers ending in 3: add "rd" (3rd, 23rd, 33rd) — except 13th All others: add "th" (4th, 5th, 6th, 7th, 8th, 9th, ...

Read More

Finding smallest number using recursion in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 262 Views

We are required to write a JavaScript function that takes in an array of Numbers and returns the smallest number from it using recursion. Let's say the following are our arrays: const arr1 = [-2, -3, -4, -5, -6, -7, -8]; const arr2 = [-2, 5, 3, 0]; Recursive Approach The recursive solution uses a helper function that compares the first element with the rest of the array: const arr1 = [-2, -3, -4, -5, -6, -7, -8]; const arr2 = [-2, 5, 3, 0]; const min = arr => { ...

Read More

Subarrays product sum in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 188 Views

We are required to write a JavaScript function that takes in an array of numbers of length N such that N is a positive even integer and divides the array into two sub arrays (say, left and right) containing N/2 elements each. The function should calculate the product of each subarray and then add both the results together. Example If the input array is: const arr = [1, 2, 3, 4, 5, 6] The calculation would be: (1*2*3) + (4*5*6) 6 + 120 126 Using Array.reduce() Method Here's ...

Read More
Showing 31–40 of 5,881 articles
« Prev 1 2 3 4 5 6 589 Next »
Advertisements