数组的特点:长度固定,查询复杂度O(1) ,增加或者删除一个元素是很慢
经常问数组的反转、数组的排序或者查找数组中的一个元素等。
1. How to find the missing number in integer array of 1 to 100?
2. How to find duplicate number on Integer array in Java?
3. How to check if array contains a number in Java?
4. How to find largest and smallest number in unsorted array?
5. How to find all pairs on integer array whose sum is equal to given number?
6. How to find repeated numbers in an array if it contains multiple duplicates?
7. Write a program to remove duplicates from array in Java?
8. How to sort an array in place using QuickSort algorithm?
9. Write a program to find intersection of two sorted arrays in Java?
10. There is an array with every element repeated twice except one. Find that element?
11. How to find kth smallest element in unsorted array?
12. How to find kth largest element in unsorted array?
13 How to find common elements in three sorted array?
14. How find the first repeating element in an array of integers?
15. How to find first non-repeating element in array of integers?
16. How to find top two numbers from an integer array?
17. How to find the smallest positive integer value that cannot be represented as sum of any subset of a given array?
18. How to rearrange array in alternating positive and negative number?
19. How to find if there is a sub array with sum equal to zero?
20. How to remove duplicates from array in place?
21. How to remove a given element from array in Java?
22. How to merge sorted array?
23. How to find sub array with maximum sum in an array of positive and negative number?
24. How to find sub array with largest product in array of both positive and negative number?
25. Write a program to find length of longest consecutive sequence in array of integers?
26. How to find minimum value in a rotated sorted array?
27. Given an array of of size n and a number k, find all elements that appear more than n/k times?
28. How to reverse array in place in Java?
29. Difference between array and linked list data structure?
30. How to check if array contains a duplicate number?
代码:
1.如何在1到100的整数数组中查找缺少的数字
package com.cyw.interview.array;
import java.util.Arrays;
import java.util.BitSet;
/**
* Java Program to find missing numbers
*
* we have numbers from 1 to 100 that are put into an integer array, what's the
* best way to find out which number is missing?
*/
public class FindMissingNumInArray {
public static void main(String[] args) {
// four missing number
printMissingNumber(new int[] { 1, 2, 3, 4, 9, 8 }, 10);
// Only one missing number in array
int[] iArray = new int[] { 1, 2, 3, 5 };
int missing = getMissingNumber(iArray, 5);
System.out.printf("数组 %s 在长度 %d 中丢失的数字是 %n", Arrays.toString(iArray),5, missing);
}
public static void printMissingNumber(int[] numbers, int count) {
int missingCount = count - numbers.length;
var bitSet = new BitSet(count);
for (var number : numbers) {
bitSet.set(number - 1);
}
System.out.printf("数组 %s 在长度为 %d 中丢失的数字是 %n", Arrays.toString(numbers),count);
var lastMissingIndex = 0;
for (var i = 0; i < missingCount; i++) {
lastMissingIndex = bitSet.nextClearBit(lastMissingIndex);
System.out.print(++lastMissingIndex+",");
}
System.out.println();
}
/**
*
* */
public static int getMissingNumber(int[] numbers, int totalCount) {
int expectedSum = totalCount * (totalCount + 1) / 2;
int actualSum = 0;
for (int i : numbers) {
actualSum += i;
}
return expectedSum - actualSum;
}
}
关于这个问题,当已知只有一个丢失的数字的时候,可以用理论值的和减去时间值的和,从而找到丢失的数字。
然而实际中经常遇到的问题是,查找不止一个丢失的数字时,使用位图BitSet 来解决。
使用BitSet解决的类似问题有:
1、统计40亿个数据中没有出现的数据,将40亿个不同数据进行排序。
2、现在有1千万个随机数,随机数的范围在1到1亿之间,要求写出一种算法,将1到1亿之间没有在随机数中的数求出来。
3、有一个40G大小的文件,里面存的是32位正整数记录,我需要查找其中一个文件,问如何查找?