Discover the longest substring in a string without any repeated characters

I'm looking to identify the longest substring within a given string.

const str = "aassqwertybbvvqwertyuivv";

const longestSubstring = str.split("").reduce((prevValue, currValue, index, arr) => {
    let substr = ""
    if (prevValue !== currValue) {
        substr = currValue + substr;
    }
    return substr;
}, "");
console.log(longestSubstring)

The expected output is 8 (qwertyui).

Currently, it only provides me with the last substring found.

Answer №1

Is this what you were looking for?

const string = "aassqwertybbvvqwertyuivv"

const findLongestSubstring = (max, current) => current.length > max.length ? current : max

const longestSubstring = string.replace(/(.)\1+/g, ' ')
  .split(' ')
  .reduce(findLongestSubstring, '')
  
console.log(longestSubstring)

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

Discover the method for selecting an element within a table that includes a style attribute with padding using Jquery

Looking for a way to identify a td element with the padding style in a table I've attempted to locate a td element with the padding style attribute in a table $(Table).find('td[style="padding:"]') or $(Table).find('td[style] ...

Body section CSS selector

Can a CSS selector be included within the body section of an HTML document? Here's an example of my code (although it is not functioning as expected): <html> <head> </head> <body> <div style= "a[target=_blank] {backgroun ...

Can the meta viewport be eliminated using a script?

I currently do not have access to the HTML file on the server side, but I can make changes by adding additional HTML to the website. My issue is with a non-responsive site listed below, which now requires me to zoom in after it loads due to the new viewpor ...

Utilizing asynchronous programming for scenarios where two requests need to be sent, with the response from the first request being required for the second request

I have an async function that looks like this: exports.myFunction = async (req, res, next) => { if (some condition) { next() } try { const results = await axios.get(`https://a-domain.com/url/path`); const info = results.data; c ...

Using jQuery to dynamically assign default selection in HTML

Currently, I have a line of code that dynamically sets the default selection of a selection box in html using jQuery. var y=...a value from the DB; this.$(".status option[value=y]").attr("selected", "selected"); The issue is that it only works if I ...

swap out an element in an array with an extra element

My array contains elements with both id and des properties. I would like to add an additional property like value:0 to each object in the array. I achieved this using a loop. let data = [ { "id": 1001, "des": "aaa" }, { ...

Adjusting the dimensions of a rectangle using jQuery: A step-by-step guide

I am encountering an issue while attempting to adjust the width and height of a rectangle using jQuery. The error message I receive is "Uncaught TypeError: Cannot read property 'scrollWidth' of null" Below you can find the code snippet in questi ...

What is the best way to make an animated image move?

Seeking guidance on how to achieve a particular effect like the one showcased at I'm specifically interested in replicating the block movement upon hover and the change in block coordinates when the cursor is not active. Does anyone know of a suitabl ...

The state initialization process is beginning with an [object Object] being passed into the setState function

Within the async function provided below, I am invoking stationData to verify that an array of objects is being passed into bartData (which currently consists of an empty array). Upon exploration, I have observed a response containing the anticipated array ...

Angular Reactive Forms may not properly update other inputs when binding a control to multiple inputs

While working with reactive forms, I encountered an issue where accessing the same control in multiple inputs seemed to result in one-way data binding (input-to-model). If I make edits in one input, it updates the model correctly but does not refresh the o ...

Javascript JSON is unable to retrieve the tag

Looking at this JSON structure: { "armament": { "air_armament": [ { "names": { "russian_name": "R-60", "NATO_name": "AA-8 Aphid" }, "weight": " ...

obtain the selected value from the radio button

While working on the server side, I dynamically created radio buttons with the following code: RadioButton button1 = new RadioButton(); button1.ID = question.Name + "_Radio1"; button1.Text = "Yes"; RadioButton button2 = new RadioButton(); button2.ID = qu ...

Update the page content once the popover is closed. Working with IONIC 3

In my application, there are 4 tabs, each displaying different data based on a specific configuration. The header of the page includes a popover component with settings options. When a user adjusts the settings and returns to a tab page, the content on th ...

From javascript to utilizing ajax calls to interact with php scripts,

I am currently working on a page called edit.php where I need to pass a JavaScript variable to a modal window containing PHP in order to execute a query and retrieve data. Unfortunately, my experience with Ajax is limited and I haven't been able to fi ...

Creating dynamic parameters in Nuxt JS for nested pages

Struggling with creating a dynamic param for my nuxt JS page's correct folder structure. The URL I'm aiming for is similar to: https://example.com/landing/123 The number 123 is the dynamic parameter I want to retrieve using this.$route. I&apos ...

There appears to be an issue with Google Analytics not generating __utm.gif requests, yet no error messages are

I'm encountering an issue with implementing Google Analytics. Despite extensive research, I haven't been able to find a resolution. My objective is to include the user's email address as a custom variable in Google Analytics. I have integra ...

What is the best way to display three arrays in a single table, with each array occupying its own column?

There are 3 arrays with different elements: $arr1 = [x, y, z, w]; $arr2 = [apple, banana, orange, pear]; $arr3 = [1, 2, 3, 4]; Each array has the same number of elements as the others: count($arr1) == count($arr2) == count($arr3); I would like to dis ...

Scrollable tabs with Ionic

Hey there! I was researching before starting my project to see if Ionic has a feature similar to scrollable tabs. I noticed this in the fitrpg app, which was built with Ionic, but I'm unsure if it's a custom design. I plan to use it for a list si ...

When using `setState()`, ensure that you are updating a component that is already mounted or in the process of mounting. If you receive this error, it

When using WebSocket to communicate with my server, I call the handleSubmit() function to send data and update my state based on the received response. Everything works fine initially. Upon calling componentWillUnmount, I stop sending data to the websocke ...

Tips on how to lock the ag-grid to the highlighted row's position and force it to scroll

We have integrated ag-grid into our system, and I am facing an issue with displaying the scrollbar in a child window based on the selected row position in ag-grid. Please refer to the Plunkr link below and click on source_id which will lead you to an edit ...