/ / एक सरणी में डुप्लिकेट मान की गणना - जावा

एक ऐरे - जावा में डुप्लिकेट मानों की गणना करना

मुझे लगता है कि गणना करने के लिए एक कार्यक्रम लिखने के लिएएक सरणी में डुप्लिकेट। यदि कोड एक ही संख्या के दो हैं तो कोड काम करता है। हालांकि, एक ही संख्या के तीन या अधिक होने पर एक त्रुटि है। मैं इसकी शुरुआत कैसे करूं?

public class Duplicate
{
public static void main(String[] args)
{
int[] list = new int[]{1,2,3,4,5,6,7,8,8,8,9,10};

int sum = 0;
for(int count=1; count<list.length; count++)
{
if(list[count-1]==list[count])
{
sum = list[count-1] + list[count];
System.out.println("Duplicate found: " + list[count] + " " + "Sum of the duplicate value is " +sum);
}
}
}
}

उत्तर:

उत्तर № 1 के लिए 4

यह आपके लिए टोटका करेगा

public static void main(String[] args)
{
int[] array = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 8, 8, 9, 10 };

int sum = 0;
for (int j = 0; j < array.length; j++)
{
for (int k = j + 1; k < array.length; k++)
{
if (k != j && array[k] == array[j])
{
sum = sum + array[k];
System.out.println("Duplicate found: " + array[k] + " " + "Sum of the duplicate value is " + sum);
}
}
}
}

आशा करता हूँ की ये काम करेगा!


जवाब के लिए 8 № 2

यहाँ एक जावा-8-शैली, कार्यात्मक दृष्टिकोण है:

int[] array = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 8, 8, 9, 10 };

// Create a Stream<Integer> from your data
IntStream.of(array)
.boxed()

// Group values into a Map<Integer, List<Integer>>
.collect(Collectors.groupingBy(i -> i))

// Filter out those map values that have only 1 element in their group
.entrySet()
.stream()
.filter(e -> e.getValue().size() > 1)

// Print the sum for the remaining groups
.forEach(e -> {
System.out.println(
"Duplicates found for : " + e.getKey() +
" their sum being : " + e.getValue()
.stream()
.collect(Collectors.summingInt(i -> i)));
});

आपके इनपुट के लिए, यह पैदावार:

Duplicates found for : 8 their sum being : 24

इस समाधान के बारे में अच्छी बात यह है कि यह अनियंत्रित के लिए काम करता है int[] जैसा था वैसा ही। जैसे के लिये...

int[] array = new int[] { 1, 10, 3, 2, 3, 4, 5, 8, 6, 7, 8, 8, 8, 9, 10 };

उत्पादन होगा ...

Duplicates found for : 3 their sum being : 6
Duplicates found for : 8 their sum being : 32
Duplicates found for : 10 their sum being : 20

जवाब के लिए 4 № 3

यदि आप जावा 8 के लिए संग्रह पुस्तकालय, जवालांग का उपयोग करने के लिए "टी माइंड" नहीं करते हैं, तो यहां एक संक्षिप्त समाधान है:

int[] array = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 8, 8, 9, 10 };

// javaslang.collection.List
List.ofAll(array)
.groupBy(i -> i)
.entrySet()
.filter(e -> e.value.length() > 1)
.forEach(e -> {
System.out.println(
"Duplicates found for : " + e.key +
" their sum being : " + e.value.sum());
});

जैसा कि अपेक्षित था, इससे पैदावार होती है:

Duplicates found for : 8 their sum being : 24

और, इसी तरह लुकास के उत्तर के लिए "

int[] array = new int[] { 1, 10, 3, 2, 3, 4, 5, 8, 6, 7, 8, 8, 8, 9, 10 };

इसकी पैदावार होती है

Duplicates found for : 10 their sum being : 20
Duplicates found for : 8 their sum being : 32
Duplicates found for : 3 their sum being : 6

कृपया ध्यान दें कि यह कोड Javaslang 2.0.0-SNAPSHOT के साथ चलता है, जो जल्द ही जारी किया जाता है।