Count frequencies of all elements in array in O (1) Extra Space and O (n) time

Source: Internet
Author: User

Given an unsorted array of n integers which can contain integers from 1 to n. Some elements can is repeated multiple times and Some other elements can is absent from the array. Count frequency of all elements that is present and print the missing elements.

Examples:

Input:arr[] = {2, 3, 3, 2, 5}

Output:below is frequencies of all elements:

1-0

2-2

3-2

4-0

5-1

Input:arr[] = {4, 4, 4, 4}

Output:below is frequencies of all elements

1-0

2-0

3-0

4-4

The most straightforward idea for the frequency of statistics is to use a hashmap, but the use of HashMap requires an O (n) secondary storage space, and we notice that the range of values in the array is 1 to n, so it's easy to think of a problem in Leetcode, first Missing Positive Thinking: Scan the array, and put a[i] in the position of a[a[i]-1], but because we need to record the number of times each number appears, it is not enough, so we decided to use A[i] record the number of values i+1 occurrences, However, because the number of occurrences is to be separated from the value that is not scanned, the number of occurrences can be saved with negative numbers. Another detail is, suppose the array a[i] = k, then the K should be placed in the position of a[k-1], but the position of a[k-1] where the value is placed? The initial processing is to continue to put the numbers in the a[k-1] in the correct position until the end of a rotation period, but such processing increases the complexity and readability of the program, it is easy to think about how to write or error, so a simple way to do is to first save the a[k-1] in A[i], Then do not modify I until the value in A[i] is negative or 0, so the program is as follows:

Class solution{

public void Count (int[] A) {

if ((A = = null) | | (A.length = = 0)) {

Return

}

Preprocess the array, if a[i] = = i+1, a[i] =-1; if (A[i] < 1) | | (A[i] > A.length) A[i] = 0;

for (int i=0; i<a.length; i++) {

if (a[i] = = (i+1)) {

A[i] =-1;

} else {

if ((A[i] < 1) | | (A[i] > A.length)) {

A[i] = 0;

}

}

}

int i=0;

while (I < a.length) {

if (A[i] <= 0) {

i++;

} else {

Int j = A[i];

if (A[j-1] <= 0) {

A[i] = 0;

A[J-1]-= 1;

i++;

} else {

A[i] = a[j-1];

A[J-1] =-1;

}

}

}

}

}

Count frequencies of all elements in array in O (1) Extra Space and O (n) time

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.