Interview question: In an array there are 0-99 integers between 101 (array unordered), in an efficient way to find the only duplicate elements!

Source: Internet
Author: User

/*
* Problem: Find duplicate elements in 101 data
* Topics are as follows: existing 0 to 99, total 100 integers, each different, put all the numbers into an array, randomly arranged.
* Array length 101, extra number is 0 to 99 any number (unique repeating number)
* Please find this duplicate number.
Analysis
* A: Construct the array
* B: Add the array elements in
* C: The elements in the array are scrambled (randomly distributed)
* D: Find the duplicate element

*/

There are three ways to do this: the first one is sorted by the Exchange sort, the second is the array and then minus 0-99, and the third kind of XOR operation finds out

The source code is as follows:

Package Java basic topic; */* Problem: Find duplicate elements in 101 data * Topics are as follows: existing 0 to 99, total 100 integers, each different, put all the numbers into an array, randomly arranged. * Array length 101, extra number is 0 to 99 any number (unique repeating number) * Please find this duplicate number * Analysis: * A: The array is constructed * B: Add the array elements into the * C: The elements of the arrays are scrambled ( Random distribution) * D: Find duplicate elements */public class A {public static void main (string[] args) {int key = (int) (Math.random () * 100);//Generated A number from 0 to 99 System.out.println ("key=" +key);//This randomly generated repeating element [0,99]//A: Construct the array out int[] arr = new int[101];//B: Add an integer element between 0 and 99 to the array for the for (int i = 0; i < arr.length-1; i++) {arr[i] = i;} ARR[100] = key;//The last element is assigned a randomly generated repeating element key, (especially note that after the array order is scrambled, then the duplicate element may not be the last) System.out.println ("The original order of the array is:");p rint ( ARR);//Call scrambled Array method random (arr); System.out.println ("The scrambled order of the array is:");p rint (arr); method1 (arr); method2 (arr); method3 (arr);} Generates a random number between 0 and 99 public static void Random (int[] arr) {//C: Elements of an array are scrambled (randomly distributed)//public static double random () in the Math class: Generates random  Number, range is [0.0,1.0]//int num = (int) (Math.random () *101),//num value range is [0,100]//using for Loop 1000 times for (int i = 0; i <; i++) {int INDEX1 = (int) (Math.randOM () * 101);//randomly generate 2 subscripts, then exchange the value of two subscript corresponding array, repeat 1000 times basically disturb the value of the array int index2 = (int) (Math.random () * 101);//interchange element int temp = Arr[in DEX1];ARR[INDEX1] = arr[index2];arr[index2] = temp;}} Output function print public static void print (int[] arr) {int len = 0;for (int i = 0; i < arr.length; i++) {System.out.print (a Rr[i] + "\ t"); Len++;if (len% 5 = = 0) {System.out.println ();}} System.out.println ("\ n--------------------------------");} Solution One: Find duplicate elements with swap sort (inefficient) public static void Method1 (int[] arr) {a:for (int i = 0; i < arr.length-1; i++) {for (int j = i + 1; J < Arr.length; J + +) {if (arr[i] = = Arr[j]) {System.out.println ("the repeating element found with interchange sorting is:" + arr[i]); break a;//break can only jump out of the current layer of loops}}}}// Solution Two: Find the sum of the array minus 0 to 99, the remaining/* * Next, we use a more coincidental approach, we come back to the topic: the existing 0 to 99, a total of 100 numbers, and there is a duplicate data, this data is between 0 to 99 ideas: * A: We add up the value of all the elements of the entire array, is it the sum of 0 to 99 and that repeating element? * B: We take this result minus 0-99 and, then the end result is that the repetition of the element * way two of the drawback is: if the calculation of the data is particularly many, there will be data overflow, so bad! A better way to do this is to use XOR or solve!!!!!! */public static void Method2 (int[] arr) {int sum = 0;for (int i = 0; i < Arr.leNgth; i++) {sum + = Arr[i];}  Use sum minus 0--99 and the rest is the duplicate number for (int i = 0; i < arr.length-1; i++) {//sum-= arr[i];//cannot write this, because the array is scrambled, the repeating element is not necessarily the last one. For example, 12345 3, after 3124 5 is upset, the last one becomes 5sum-= i;} System.out.println ("To find the duplicate element by summing and subtracting the sum) is:" + sums);} /* * Way three: The difference or one of the characteristics is: x^0^1^2...^99^0^1^2...^99=x * Look at an equation: 0^1^2^...^m...^99 ^m^ 0^1^2...^m...^99 is equal to M, you can find the value of the repeating element * so, We use all elements in the array that are different or later than the first element. */public static void Method3 (int[] arr) {for (int i=1;i<arr.length;i++) {arr[0] = arr[0]^arr[i];} We again put arr[0] saved results and 0,1,2...99 data xor once for (int i=0;i<arr.length-1;i++) {//arr[0] = arr[0]^arr[i];//cannot write because the array is scrambled , the repeating element is not necessarily the last one, such as 12345 3, after 3124 5, the last one becomes 5arr[0] = arr[0]^i;//Note is not arr[0]^arr[i], the array is scrambled in order}system.out.println ("The repeating element is: "+arr[0]);}}

Output

key= 49 The original order of the array is: 01234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 6061626364656667686970717273747576777879808182838485868788899091929394959697989949----------------------------- The order in which the---array is scrambled is: 871635737131729511481557891338194541225915589046324060755224188367659786439339738070204964786896986512910429 963502176941282308491337533655698222144488591945922734775604779746226236166388584417--------------------------- -----The duplicate elements found in the swap sort are: 49 find the repeating element by summing and subtracting the elements: 49 The repeating element is: 49



Interview question: In an array there are 0-99 integers between 101 (array unordered), in an efficient way to find the only duplicate elements!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.