Home Web Front-end JS Tutorial Introducing the dichotomy method in js and the example code for deduplication

Introducing the dichotomy method in js and the example code for deduplication

Jul 17, 2017 pm 04:04 PM
Remove duplicates

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<body>
		
	</body>
	<script type="text/javascript">
		var arr =[1,2,3,4,5,6,7,8,9,0,8,5,5,4,3];
		//创建一个数组
		function findInArr(arr,n){
			//循环数组中的每一项如果它的每一个i项与n相等就返回继续执行
			for (var i=0;i<arr.length;i++){
				if (arr[i] == n){
					return true;
				}
			}
			    return false;
		}
		function removeDup(arr,s,e){
//			判断这个数组,的开始顺序,和这个数组是不是首项和尾项相等
			if (s>e) {
				return false;
			} else if(s==e){
				return [arr[s]];
			}
//			将数组进行二分,找到中间项,将数组分为两部分
			var c= Math.floor((s+e)/2);
			var l = removeDup(arr,s,c);
			var r = removeDup(arr,c + 1,e);
			for (var i=0;i< r.length; i++) {
				if (!findInArr(l,r[i])) {
					l.push(r[i])
				}
			}
			return l;
		}
		console.log(removeDup(arr,0,arr.length-1))
	</script>
</html>
Copy after login

Algorithms are a wonderful thing, and I hope we can communicate more with them.

The above is the detailed content of Introducing the dichotomy method in js and the example code for deduplication. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1670
14
PHP Tutorial
1274
29
C# Tutorial
1256
24
Revealing the efficient data deduplication method in Pandas: Tips for quickly removing duplicate data Revealing the efficient data deduplication method in Pandas: Tips for quickly removing duplicate data Jan 24, 2024 am 08:12 AM

The secret of Pandas deduplication method: a fast and efficient way to deduplicate data, which requires specific code examples. In the process of data analysis and processing, duplication in the data is often encountered. Duplicate data may mislead the analysis results, so deduplication is a very important step. Pandas, a powerful data processing library, provides a variety of methods to achieve data deduplication. This article will introduce some commonly used deduplication methods, and attach specific code examples. The most common case of deduplication based on a single column is based on whether the value of a certain column is duplicated.

How to optimize collection sorting and deduplication performance in Java development How to optimize collection sorting and deduplication performance in Java development Jul 02, 2023 am 11:25 AM

In Java development, collection sorting and deduplication are common requirements. However, performance often becomes an issue when dealing with large data collections. This article will introduce some optimization techniques to help improve the performance of collection sorting and deduplication. 1. Use appropriate data structures. In Java, the most commonly used data structures are ArrayList and HashSet. ArrayList is suitable for situations where the order of elements needs to be maintained, while HashSet is suitable for situations where duplication needs to be eliminated. In sorting and deduplication scenarios, we can use

How to remove duplicates in word How to remove duplicates in word Mar 20, 2024 pm 02:13 PM

Sometimes when we use word office software to operate and edit files, some content is repeated. How can we quickly find the repeatedly entered information and then delete the repeated content? It is easy to find duplicates in an Excel spreadsheet, but will you find duplicates in a word document? Below, we will share how to remove duplicates in word, so that you can quickly find duplicate content and perform editing operations. First, open a new Word document and enter some content in the document. Consider inserting some repetitive parts to help demonstrate operations. 2. To find duplicate content, we need to click [Start]-[Search] tool in the menu bar, select [Advanced Search] in the drop-down menu, and click

What are the methods to remove duplicates in pandas? What are the methods to remove duplicates in pandas? Nov 22, 2023 am 11:55 AM

The pandas deduplication methods are: 1. Use the drop_duplicates() method; 2. Use the duplicated() method; 3. Use the unique() method; 4. Use the value_counts() method. Detailed introduction: 1. Use the drop_duplicates() method to delete duplicate rows in the data frame and return a new data frame. It can set parameters to control how to perform deduplication, such as specifying the retention order and deduplication after deduplication. Time comparison columns and so on.

How to perform deduplication operation after the PHP array is shuffled? How to perform deduplication operation after the PHP array is shuffled? May 02, 2024 pm 01:33 PM

In PHP, you can use the following steps to disrupt the order of the array and then perform deduplication operations: Use the shuffle() function to disrupt the order of the array. Use the array_unique() function to deduplicate the array and remove duplicate elements.

How to achieve deduplication of data in PHP arrays? How to achieve deduplication of data in PHP arrays? Apr 26, 2024 pm 06:51 PM

Three methods to deduplicate PHP arrays: use the array_unique() function to remove duplicate values ​​based on element values ​​and retain the key value order. Use the array_filter() function to remove duplicate elements based on the conditions of the callback function. Use the SplObjectStorage class to take advantage of the uniqueness of objects to achieve array deduplication and retain key-value associations.

How to deal with duplicate data in Oracle database and get only one piece? How to deal with duplicate data in Oracle database and get only one piece? Mar 08, 2024 pm 04:39 PM

In Oracle database, it is a common requirement to deal with duplicate data and keep only one record. This situation usually occurs when there are duplicate data in the data table, but we only need to keep one of them and remove the remaining duplicate data. Oracle provides some methods to achieve this functionality, including using the ROWID and ROW_NUMBER functions. The following will introduce in detail how to handle duplicate data in Oracle database and only fetch one piece, and provide specific code examples. Method 1: Use ROWID in Ora

An in-depth analysis of five practical methods for deduplicating Java arrays An in-depth analysis of five practical methods for deduplicating Java arrays Dec 23, 2023 am 09:21 AM

An in-depth analysis of five practical methods for deduplicating Java arrays. In Java, processing arrays is a very common operation. Array deduplication is a problem often encountered in actual development. This article will provide an in-depth analysis of five practical methods for Java array deduplication and provide specific code examples. 1. Use HashSet to remove duplicates. HashSet is a collection in Java that has the function of automatic deduplication. We can use the characteristics of HashSet to add elements in the array to HashSet to achieve the effect of deduplication.

See all articles