Home > OS >  Finding duplicates in an array using while loop, and add the sum of the duplicate elements
Finding duplicates in an array using while loop, and add the sum of the duplicate elements

Time:05-26

I used the for loop code found on the internet but my professor asked me to convert it to while loop. I also don't have any idea how to code the sum part. We can't use any advanced stuff like def. I only started to code for class using python and I've honestly been stuck and haven't been catching up on the lessons. Any help is appreciated, thank you.

import array as arr
a=arr.array('i', [])
x=0

#size of array
arrsize=int(input("\nPlease Enter the number of elements: "))

#elements
print("\nPlease Enter " str(arrsize) " elements")
while(x<arrsize):
    num=int(input("Enter a number: "))
    a.insert(x,num)
    x =1
print("The elements are: ",a)

#duplicate elements
for i in range (0, len(a)):
    for j in range (i 1, len(a)):
        if(a[i]==a[j]):
            print("\nThe duplicate elements are: ",(a[j]))

CodePudding user response:

The statement

for i in range(x, y):
   # Code to loop through here

Is essentially saying that you will loop through the loop code one time for every number i between x and y, including x and excluding y.

We can write an equivalent statement with a "while" loop. But we'll need to manage the variable i separately (including initializing it and incrementing it manually)

i = x
while i < y:
   # Code to loop through here
   i  = 1

Now that you know how these 2 loop types are equivalent, you can use this equivalency to translate your code from a for loop to a while loop. You will need to do it twice; once for each for-loop in your code. I definitely recommend doing this on your own as it's an important part of the learning process!

CodePudding user response:

The concept is the same as for the for-loop ,only difference will be increasing the indexes manually since while is used for condition based loops.

sum = 0 #to add each duplicates value 
dup=[]  #store each duplicate if needed 

# increase the values of i,j manually 
i = 0
while(i< len(a)):
    j=i 1
    while(j<len(a)):
        if(a[i]==a[j]):
            #if duplicate found then add 
            sum = sum   a[i]
            dup.append(a[i])
        j=j 1
    i=i 1       
        

print("\nThe duplicate elements are: ",dup)
print("Sum of of the duplicates :" , sum)

CodePudding user response:

You don't have to import any modules, Python's builtin list is sufficient for your application. Start with an empty duplicates list, dup_sum = 0, and go through all elements of your array; if some element is not in duplicates and its count is greater than 1, insert it into duplicates and add the sum of occurrences to dup_sum. Finally, print the info. you want.

a = []
x = 0
# size of array
arrsize = int(input('Please Enter the number of elements: '))

# elements
print(f'Please Enter {arrsize} elements')
while x < arrsize:
    num = int(input('Enter a number: '))
    a.append(num)
    x  = 1
print(f'The elements are: {a}')

# duplicate elements
duplicates = []
dup_sum = 0
i = 0
while i < len(a):
    d = a[i]
    if d not in duplicates:
        dcount = a.count(d)
        if dcount > 1:
            dup_sum  = d * dcount
            duplicates.append(d)
    i  = 1
print(f'The duplicate elements are {duplicates}')
print(f'Sum of duplicates = {dup_sum}')

Here is a sample run of the program:

Please Enter the number of elements: 5
Please Enter 5 elements
Enter a number: 1
Enter a number: 2
Enter a number: 2
Enter a number: 3
Enter a number: 3
The elements are: [1, 2, 2, 3, 3]
The duplicate elements are [2, 3]
Sum of duplicates = 10
  • Related