Eliminate arrays with duplicate items in multiple dimensions

I keep encountering this question, however my case seems to be unique as it hasn't been addressed in any of the solutions I've found on SO.

My challenge involves a multidimensional array with duplicate arrays that I need to eliminate.

[
    [8, 12],
    [8, 17],
    [8, 92],
    [8, 12]
]

The array [8, 12] is repeated. How can I remove this duplicate?

Answer №1

Consider the following code snippet:

let array1 = [[8,12],[8,17],[8,92],[8,12]];
let array2 = new Array();

for (let i=0; i<array1.length; i++) {
 let e1 = array1[i];
 let exists = false;
 for (let j=0; j<array2.length; j++) {
  if (array2[j][0] == e1[0] && array2[j][1] == e1[1]) exists = true;
 }
 if (exists == false) {
  array2[array2.length] = e1;
 }
}

After running this code, array2 will contain unique elements from array1 without any duplicates. Although this may not be suitable for real-time game programming due to potential inefficiencies, it should still function correctly. Please note that I have not tested the code, so there may be unintended errors.

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

Secured interactions following authentication using React Query and NextAuth

After successfully signing in to my Nextjs app using NextAuth, I encountered an issue with sending authenticated requests to my API. The data returned after signing in appears to be for an unauthenticated user. I suspect that the problem lies in React Que ...

What is the best way to make a vertical line using the CSS code provided in the link?

Check out this cool horizontal line design I found on stackoverflow: Click here for CSS code to create the line I really like the look of the line. Here's how I implemented it on my website: hr.fancy-line { border: 0; height: 5px; } hr ...

Why does one HTML link function while the other does not?

After creating links that connect two HTML files, I encountered an issue where one link does not work. The situation involves a folder named aiGame containing the aiindex.html file along with ai.js and ai.css. It is worth noting that the index.html file is ...

Swap out a web link for an embedded iframe

Currently working on updating internal links: <div class="activityinstance"> <a href="http://website.com/hvp/view.php?id=515512">activity</a> </div> The goal is to convert them into: <div class="activityinstance"> ...

Can the Firefox Web Console be accessed while in headless mode?

Is it possible to interact with the Firefox console when starting Firefox in headless mode? Alternatively, is there a way to programmatically access the console in scripts? My attempts so far: I have been experimenting with the Javascript bindings to Se ...

How can I trigger an event to append a UL element using

Can anyone suggest a more elegant solution for handling an append/remove event from an element without having to manually trigger an event or resorting to other methods? It would be great if there was a way to do something like this: $('#id').o ...

What is the best way to send multiple variables using Ajax?

I have developed an ajax code for a comment box that triggers PHP code on another page to insert comments into a table. Although this test code is functioning properly, I now want to include additional information such as userTo, userFrom, and postID along ...

Error C3646 in C++: `jarArray` is not recognized as a specifier override

Here's a simple one for you... I've got an array of objects (jars) that I defined within a header file (box). #include "Jar.h" using namespace std; class box { public: box(); void searchForPart(string part); private: int numberOfJars; ...

Can anyone explain why the Splice function is removing the element at index 1 instead of index 0 as I specified?

selectedItems= [5,47] if(this.selectedItems.length > 1) { this.selectedItems= this.selectedItems.splice(0,1); } I am attempting to remove the element at index 0 which is 5 but unexpectedly it deletes the element at index ...

flatpickr allows you to synchronize the dates of two date pickers by setting the date of the second one to match the date

Currently utilizing flatpikr from . I'm looking to have the initial date of the return picker set to the same date as the outbound picker upon closing the outbound picker. The code I've written achieves this functionality, but there's an iss ...

Tips on how to turn off HTTPS-only fetch requests in Expo React Native/Node.js

Recently, I encountered an issue while attempting to use the fetch API to communicate with my localhost server. The fetch request resulted in the following error: [Unhandled promise rejection: TypeError: Network request failed] - node_modules\whatwg- ...

Unable to close a jQuery dialog box after an AJAX call has been successful

function sendMessage(){ $.ajax({ beforeSend: function(){ $("#loading").dialog('open').html("<p>Please Wait...</p>"); }, url: "${pageContext.request.contextPath}/Whoopiee ...

Unable to load images on website

I'm having trouble showing images on my website using Node.js Express and an HBS file. The image is not appearing on the webpage and I'm seeing an error message that says "GET http://localhost:3000/tempelates/P2.jpg 404 (Not Found)" Here is the ...

creating a nested JavaScript object within another object

I need to create an object using the angular.forEach() function and then push another object while initializing all values to false. However, the current approach is causing errors. How can I achieve this correctly? Using "item.id" and "index.id" does not ...

Clicking using jQuery - an inquiry

There is a problem that I need help with. This is the code causing the issue: $("#button").click(function(event){ $("#threads").html("hi"); }); Currently, when I click the button, the text "hi" only appears for 1 second and then disappears. I want it ...

Incorporating Javascript into a .Net MVC 3 Project

It seems like there should be a straightforward solution to my question, but I'm struggling with it. I have an animation.js file that includes dependency_1.js and dependency_2.js in an include folder. Within my animation.js file, I load these dependen ...

Produced inputs and preset values

I have a question regarding the use of generated input elements in my App's form. I want to keep it as simple as possible, which is why I am using native form reset for these elements. It appears that the 'default value' becomes bound to th ...

Encountering a platform error when utilizing Google Cloud Function

Currently, I am working on developing an application using flutter and implementing cloud functions through firebase to update a specific number in Firestore. index.js // Utilizing the Cloud Functions for Firebase SDK to create Cloud Functions and establi ...

Iframe navigation tracking technology

Let's say I have an iframe element in my HTML document. <html> <body> This is my webpage <button> Button </button> <iframe src="www.example.com"></iframe> </body> </html> If a user clicks on links wi ...

Error in Next.js 13 due to Prisma table mapping causing hydration issues

I recently created a basic project in Next.js 13 and encountered a Hydration Error even though my project is not doing much. The code snippet below seems to be the cause of the issue: import { PrismaClient } from "@prisma/client"; export default ...