Parse, find the repeating element in the array, Java implementation.
Data structure and algorithm analysis: parsing, finding repeating elements in an array
Problem Description: A structured data that assumes that an array of orders (such as ascending) is ordered in advance in some order, with no rules, repeated occurrences of some identical element, and an orderly array of data:
data = {"A", "a", "B", "C", "C", "D", "D", "D"}
In the data array, it has been sorted in ascending order A-Z, but no rules are repeated for the elements inside the array:
' A ' in the array position 0,1 (note: 0,1 refers to subscript, the same below) repeated two times;
' B ' does not repeat;
' C ' repeats two times in the array position 3,4;
' D ' repeats three times in the 5,6,7 position.
For such data structures, the design algorithm and code implement to find and parse the repeating elements in the array.
Algorithm application scenario: one of the scenarios for this algorithm is that it will be involved in the operation of the contacts contact. For example, before I wrote an article: "Android based on Pinnedsectionlistview to achieve contact contacts" (Link address: http://blog.csdn.net/zhangphil/article/details/ 47271741, this article is related to address Book sorting, you need to follow the first character of the contact name of the group. Usually, a phone's address book contains several contacts, each contact has a surname, if according to the first character of the last name (for example, the Chinese surname ' Zhang ', Zhang, the first character is ' Z ') in ascending order, will form the data structure as described above, and then group the contacts. For example, all contacts are named under the ' Z ' group, making it easy for users to quickly locate one of the contacts in their contacts. Therefore, to set up a data model for this real-world application scenario is the computational problem to be solved by this algorithm.
A code implementation (Java) algorithm is now given:
Import Java.util.arraylist;public class Test {//raw data. Suppose that the data elements in the database array have been sorted in some order. However, the data elements in the array appear repeatedly. Our goal is to find and parse an element that recurs in the data array. For example, in this data array, the element ' C ' is repeated two times in the array position 2,3. Attention! Some elements do not recur, such as element ' B '. Private string[] data = {"A", "a", "B", "C", "C", "D", "D", "D"};//stores the well-categorized elements. Private arraylist<group> groups = new arraylist<group> ();//core algorithm implementation. public void Find () {///cursor Indexint index = 0, j = 0;while (Index < data.length) {Group group = new Group (); group.title = Data[index]; String t = group.title; arraylist<string> children = new arraylist<string> (); for (j = index; J < Data.length; J + +) {String child = Data[j];if (T.equals (Child)) {///At the same time record the repeating element in the original array subscript J, easy to examine and evaluate the results. Children.add (child + "@" + j);} else {break;}} Forward cursor Indexindex = J;group.children = Children;groups.add (group);}} Output results. private void print () {for (int i = 0; i < groups.size (); i++) {Group g = groups.get (i); System.out.println (g);}} Construct a class yourself as a container for a set of data. The class uses a title to indicate that the group data is the set that belongs to that repeating element. //The duplicate elements under this title are loaded into the arraylist<string> children for traversal queries. Private class Group {public String title;public arraylist<string> children;//result. @Overridepublic string toString () {string str = "Group" + title + ":"; for (int i = 0; I < children.size (); i++) {str + = Children.get (i) + "";} return str;}} public static void Main (String args[]) {Test t = new Test (); T.find (); T.print ();}}
Result output:
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Parsing, finding occurrences of elements in an array (Java)