JavaScript: developing an algorithm to identify and eliminate duplicate strings

Consider the following JS array:

0 - Star Wars: A New Hope
1 - Star Wars: The Empire Strikes Back
2 - Star Wars: Return of the Jedi
3 - Star Wars: A New Hope (1977)
4 - Star Wars: Return of the Jedi (1983)
5 - Star Wars: Attack of the Clones
6 - Star Wars: Revenge of the Sith
7 - Star Wars: The Phantom Menace
8 - Star Wars: Attack of the Clones (2002)
9 - Star Wars: Revenge of the Sith (2005)

We know that some items are similar. Below is the desired array output:

0 - Star Wars: A New Hope
1 - Star Wars: The Empire Strikes Back
2 - Star Wars: Return of the Jedi
5 - Star Wars: Attack of the Clones
6 - Star Wars: Revenge of the Sith
7 - Star Wars: The Phantom Menace

What approach can be used to exclude the similar items from the array?

Thank you

Answer №1

To streamline the titles, you can extract and remove the content within parentheses, place it in a Set to eliminate any duplicates, and then convert it back into an array:

movies = [...new Set(movies.map(movie => movie.replace(/\s*\(\d+\)\s*$/g, '')))];

movies = [
'The Big Bang Theory - Fourth Season',
'The Big Bang Theory - Third Season',
'The Big Bang Theory - Second Season',
'The Big Bang Theory - First Season',
'The Big Bang Theory - First Season (2007)',
'The Big Bang Theory - Fourth Season (2010)',
'The Big Bang Theory - Second Season (2008)',
'The Big Bang Theory - Third Season (2009)',
'The Big Bang Theory: Access All Areas (2012)',
'The Big Bang Theory: It All Started with a Big Bang (2012)'];

movies = [...new Set(movies.map(movie => movie.replace(/\s*\(\d+\)\s*$/g, '')))];

console.log(movies);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

having difficulty sorting items by tag groups in mongodb using $and and $in operators

I'm currently trying to execute this find() function: Item.find({'tags.id': { $and: [ { $in: [ '530f728706fa296e0a00000a', '5351d9df3412a38110000013' ] }, { $in: [ ...

Attempting to automatically change the selected input radio button every 3 seconds using HTML

Hey there, I'm just starting out with coding and StackOverflow. I'm attempting to create a basic slider but for some reason, the images are not changing. My goal is to cycle through checked input radio buttons every 3 seconds, but something seems ...

Dependencies for Grunt tasks

I am facing some issues with a grunt task named taskA that was installed via npm. The task has a dependency on grunt-contrib-stylus, which is specified in the package.json file of taskA and installed successfully. However, when I run grunt default from the ...

Move values to the right while filling in any empty spaces with zeros

I need help repositioning the values in a list to shift them right by one position and fill index 0 with a value of 0. For example: a = [8,9,10,11] output = [0,8,9,10] I attempted using the following code: a.insert(0,a.pop()) This code shifted the elem ...

When the button is clicked, bind the value to an object using the specified key in

I'm fairly new to Vue and I'm looking to generate buttons based on my data and show their information when clicked. The 'pets' object contains keys for id and info. (My actual data is more extensive and my code is a bit more complex, bu ...

Unable to capture screenshot of hovered element using Cypress

Having an issue with taking a screenshot of an element with a hover effect. The screenshots always come out without the hover effect applied. tableListMaps.lineWithText('Hello world', 'myLine'); cy.get('@myLine').realH ...

What is the best way to manage a vuex dispatch response?

Despite feeling like the answer is right in front of me, I'm waving the white flag and seeking suggestions. The challenge lies in my login form that submits to an AWS API and reacts to the result. The trouble starts when the handleSubmit method is tr ...

A guide on accessing the first element in an array when performing multiplication within the Interval component in React.js

I am having trouble initiating an if statement from number 0 once the window is loaded, as it seems to be skipping over 0 and starting from 1 instead. Can someone please assist me in understanding how this works? showAnime = () => { let counter ...

Utilizing AngularJS to interact with RESTful Web Services

As I delve into tutorials on utilizing an API with AngularJS, I'm encountering some issues when trying to display {{greeting.id}} and {{greeting.content}}. Despite my expectations, the results are not showing up on my screen. Here is the link to my ...

How to Target HTML Tags Locally using CSS Modules in Next.js?

I am looking to implement smooth scrolling specifically on one page in my Next.js application, such as my blog. Instead of applying it to the entire site through the globals.css file, I need a way to inject scroll-behavior: smooth; into the html tag only f ...

Oops! Looks like there's a hiccup with the express-validator plugin as the validation fails due to req.checkBody not being recognized as

Currently, I am setting up a post route to handle a submit request. The code snippet provided is from my user.js route: var express = require('express'); var router = express.Router(); var multer = require('multer'); var upload = multe ...

Tips for sending form data as JSON to a servlet with AJAX:

I am struggling to send login information in JSON format to a servlet using AJAX. The issue I'm facing is that the servlet is receiving null values. Interestingly, my servlet functions properly when I transmit the data without utilizing AJAX. However, ...

Having trouble selecting the clicked element after a successful Ajax call, especially when there are multiple elements with the same name

When dealing with multiple elements that share the same class name, I am attempting to target the 'clicked' element upon a successful Ajax return. <td data-name='tom' class="status"><a href="">click< ...

Retrieve main page elements from an additional page called PageSlide

I have implemented the jquery PageSlide plugin as a menu feature on my website. The plugin opens a separate page on the side of the main page to function as a menu. However, I am facing a challenge in accessing the main page's elements from this side ...

Struggling to adjust the width of a recurring div element in PHP

I'm encountering an issue on my PHP project where I have multiple comment box divs that are repeating. Each time a user clicks on one of these divs, I want the width to change to 100%. However, my problem is that when I try to implement this functiona ...

The textgeometry element is not appearing in the three.js scene

I've inserted a boxgeometry into the scene with the intention of adding text to indicate the side of the cube. However, I am encountering difficulties in incorporating textgeometry into the scene. This is my code: const loader = new FontLoader(); loa ...

Creating an object array in Visual Basic: A step-by-step guide

Can an array of objects be created in visual basic? I'm currently working on a battle system and I would like to randomly choose a Monster object from an array when the battle begins. If this is possible, could someone please demonstrate how to stor ...

How can a row in AG-Grid be updated without causing a refresh?

I am working on adding a custom radio button feature for users to select a row. Currently, I have the following code: const Cell = (cellProps) => { const { data, node, api } = cellProps const selectedRow = () => { let { isChecked } = data ...

Why does setting the variable value as an argument in an AngularJS function result in the variable becoming undefined?

The following is the Main Controller implementation: angular.module("HomePageApp", ["BaseApp"]) .controller("MainCtrl", ["$http", "$window", "BaseService", function($http, $window, BaseService) { var self = this; self.posts = BaseSer ...

Is it possible to execute asynchronous queries within a map function in Mongoose?

Currently, I am struggling to create queries using a .map function, pushing the results to an array and then returning it. The issue is that the array always ends up empty due to the asynchronous nature of the operation. Although I attempted using async/ ...