Reputation: 21
public static void printOrganizedList(int[] array) {
int[] temp = array;
System.out.println("N Count");
for(int i = 0; i < array.length; i++) {
int count = 0;
for (int j = 0; j < array.length; j++) {
if(array[i] == array[j]) {
count++;
}
}
for(int n = i-1; n > 0; n--) {
if(array[n] == array[i]) {
break;
}
else {
System.out.println(array[i] + " " + count);
}
}
}
}
This method is made to take in an array and print the duplicate values along with the amount of times it appears in the array. Like this:
-12, 3, -12, 4, 1, 1, -12, 1, 1, 2, 3, 4, 2, 3, -12
The program output should be:
N Count
4 2
3 3
2 2
1 4
-1 1
-12 4
My issue is that no matter what I try the method always spits out the duplicate number along with its amount of repeats as many times as it is repeated. So instead of outputting
"-12 4"
It will output :
"-12 4"
"-12 4"
"-12 4"
"-12 4"
Also I'm aware that there are more advanced and efficient techniques but we haven't learned a lot of that stuff yet. Thanks in advance.
Upvotes: 0
Views: 81
Reputation: 1736
This can be easily acheived using a HashMap. You can create a Hashmap which would save the element as key and keep the number of occurrences as the value.
public static void printOrganizedList(int[] array) {
System.out.println("N Count");
HashMap<Integer, Integer> countMap = new HashMap<>();
for (int i = 0; i < array.length; i++){
if (countMap.containsKey(array[i])){
int count = countMap.get(array[i]);
countMap.replace(array[i], count + 1);
}else{
countMap.put(array[i], 1);
}
}
Iterator iterator = countMap.entrySet().iterator();
while (iterator.hasNext()){
Map.Entry mapElement = (Map.Entry) iterator.next();
int key = (int) mapElement.getKey();
int count = (int) mapElement.getValue();
System.out.println(key + " " + count);
}
}
Also the time complexity of the program that you have written goes to O(N^2) which can be a really big bottleneck when it comes to large programs.
The above program with hashmap implementation would only cost you O(N)
Upvotes: 1
Reputation: 4867
public static void main(String[] args) {
printOrganizedList(new int[] { -12, 3, -12, 4, 1, 1, -12, 1, 1, 2, 3, 4, 2, 3, -12 });
}
public static void printOrganizedList(int[] array) {
System.out.println("N\tCount");
Map<Integer, Integer> freq = new TreeMap<>();
for (int i = 0; i < array.length; i++) {
if (freq.containsKey(Integer.valueOf(array[i])))
freq.put(Integer.valueOf(array[i]), freq.get(Integer.valueOf(array[i])) + 1);
else
freq.put(Integer.valueOf(array[i]), 1);
}
for (Integer key : freq.keySet()) {
System.out.println(key + "\t" + freq.get(key));
}
}
, output
N Count
-12 4
1 4
2 2
3 3
4 2
, Another solution to match your code
public static void printOrganizedList(int[] array) {
System.out.println("N\tCount");
Arrays.sort(array);
for (int i = 0; i < array.length; i++) {
int count = 0;
// calc freq
for (int j = 0; j < array.length; j++) {
if (array[i] == array[j])
count++;
}
if (count > 1)
System.out.println(array[i] + "\t" + count);
i += count;
}
}
Upvotes: 0
Reputation: 19575
If the range of the input array is reasonable (for instance, from -12 to 12, not from Integer.MIN_VALUE to Long.MAX_VALUE), you may apply count sorting:
int min = arr[0], max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] < min) min = arr[i];
else if (arr[i] > max) max = arr[i];
}
int[] freq = new int[max - min + 1];
for (int i = 0; i < arr.length; i++) {
freq[min + i]++;
}
for (int i = 0; i < freq.length; i++) {
if (freq[min + i] > 1) {
System.out.println((min + i) + " " + freq[min + i]);
}
}
Upvotes: 0