0

I am trying to either remove the duplicate entries fetched from the external database /group them as one. Initially, my sql query had a group clause. When I used it as group clause, the query was working, but not appropriately. I then removed the group clause from the query, it now shows the duplicate entries.

I have four regions: - North, East, South,West and ALL

By default it shows all region. Now there could be a profile that appears in more than one region. So when it loads as default - all regions, the profile should only appear once there, but when we tab on the regions, the profile should also show up there if the profile belongs in that region.

At them moment, it is showing all random. Some profiles that are available in more than one region, they do appear and some they dont. Some profile that is meant to be only in one region, do appear in 2 regions.

I dont know how can I share the code or screenshots, but happy to provide more details to look at the call.

Thanks

My code:

 func removeDuplicate (sourceArray: [String]) -> [String] {
    var uniqueArray:[String] = []
    for object in sourceArray {
        if !uniqueArray.contains(object)
        {
            uniqueArray.append(object)
        }
    }
    return uniqueArray
}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {



    if indexPath.section == 0
    {
        let cell = self.bussinessTableView.dequeueReusableCell(withIdentifier: "bussinessCell", for: indexPath) as! BussinessCell

        let tableObjects:StorageSubCatagoriezTwo = subCatagorizeDataTwo[indexPath.row]


        let testing = removeDuplicate(sourceArray: [tableObjects.nameString])
        print(testing.description)

        //let charCheck = (tableObjects.address).replacingOccurrences(of: " ", with: "")


 if reg == "North"
        {
            cell.bussinessName.text = " "
            cell.addLabel.text = " "
            cell.telLabel.text = " "
            cell.emailLabel.text =  " "
            cell.webLabel.text = " "
            let tableObjects:StorageSubCatagoriezTwoNorth = subCatagorizeDataTwoN[indexPath.row]


            if tableObjects.address.characters.isEmpty != true
            {
                cell.addLabel.text = tableObjects.address
            }
            else
            {
                //nothing
            }
            if tableObjects.telephone.characters.isEmpty != true
            {
                cell.telLabel.text = tableObjects.telephone
            }
            else
            {

            }
            if tableObjects.email.characters.isEmpty != true
            {
                cell.emailLabel.text = tableObjects.email
            }
            else
            {

            }
            if tableObjects.website.characters.isEmpty != true{
                cell.webLabel.text = tableObjects.website
            }
            else{

            }
        }
        else if reg == "East"
        {
            cell.bussinessName.text = " "
            let tableObjects:StorageSubCatagoriezTwoEast = subCatagorizeDataTwoE[indexPath.row]
            cell.bussinessName.text = tableObjects.nameString
            if tableObjects.address.characters.isEmpty != true
            {
                cell.addLabel.text = tableObjects.address
            }
            else
            {
                //nothing
            }
            if tableObjects.telephone.characters.isEmpty != true
            {
                cell.telLabel.text = tableObjects.telephone
            }
            else
            {

            }
            if tableObjects.email.characters.isEmpty != true
            {
                cell.emailLabel.text = tableObjects.email
            }
            else
            {

            }
            if tableObjects.website.characters.isEmpty != true
            {
                cell.webLabel.text = tableObjects.website
            }
            else{

            }
        }
        else if reg == "South"
        {
            cell.bussinessName.text = " "
            let tableObjects:StorageSubCatagoriezTwoSouth = subCatagorizeDataTwoS[indexPath.row]
            cell.bussinessName.text = tableObjects.nameString
            if tableObjects.address.characters.isEmpty != true
            {
                cell.addLabel.text = tableObjects.address
            }
            else
            {
                //nothing
            }
            if tableObjects.telephone.characters.isEmpty != true
            {
                cell.telLabel.text = tableObjects.telephone
            }
            else
            {

            }
            if tableObjects.email.characters.isEmpty != true
            {
                cell.emailLabel.text = tableObjects.email
            }
            else
            {

            }
            if tableObjects.website.characters.isEmpty != true{
                cell.webLabel.text = tableObjects.website
            }
            else{

            }
        }
        else if reg == "West"
        {
            cell.bussinessName.text = " "
            let tableObjects:StorageSubCatagoriezTwoWest = subCatagorizeDataTwoW[indexPath.row]
            cell.bussinessName.text = tableObjects.nameString
            if tableObjects.address.characters.isEmpty != true
            {
              cell.addLabel.text = tableObjects.address
            }
            else
            {
                //nothing
            }
            if tableObjects.telephone.characters.isEmpty != true
            {
                cell.telLabel.text = tableObjects.telephone
            }
            else
            {

            }
            if tableObjects.email.characters.isEmpty != true
            {
                cell.emailLabel.text = tableObjects.email
            }
            else
            {

            }
            if tableObjects.website.characters.isEmpty != true{
               cell.webLabel.text = tableObjects.website
            }

        }
        else{
            let tableObjects:StorageSubCatagoriezTwo = subCatagorizeDataTwo[indexPath.row]
            cell.bussinessName.text = tableObjects.nameString

            let charCheck = (tableObjects.address).replacingOccurrences(of: " ", with: "")

            cell.bussinessName.text = tableObjects.nameString
            cell.addLabel.text = tableObjects.address
            cell.telLabel.text = tableObjects.telephone
            cell.emailLabel.text = tableObjects.email
            cell.webLabel.text = tableObjects.website

        }


        return cell
    }

    else if indexPath.section == 1
    {
        let cell = self.bussinessTableView.dequeueReusableCell(withIdentifier: "bussinessCell2")
        cell?.textLabel?.textAlignment = .center
        switch regId
        {
        case 0:
            if subCatagorizeDataTwo.count != forAll
            {
                cell?.textLabel?.text = "Load More Data"
                cell?.textLabel?.textColor = Color.whiteColor
                cell?.backgroundColor = UIColor.darkGray

            }else{
                cell?.textLabel?.text = "No More Data Found"
                cell?.textLabel?.textColor = UIColor.darkGray
                cell?.backgroundColor = UIColor.gray
                cell?.selectionStyle = UITableViewCellSelectionStyle.none
            }
        case 1:
            if subCatagorizeDataTwoN.count != forN
            {
                cell?.textLabel?.text = "Load More Data"
                cell?.textLabel?.textColor = Color.whiteColor
                cell?.backgroundColor = UIColor.darkGray

            }else{
                cell?.textLabel?.text = "No More Data Found"
                cell?.textLabel?.textColor = UIColor.darkGray
                cell?.backgroundColor = UIColor.gray
                cell?.selectionStyle = UITableViewCellSelectionStyle.none
            }
        case 2:
            if subCatagorizeDataTwoE.count != forE
            {
                cell?.textLabel?.text = "Load More Data"
                cell?.textLabel?.textColor = Color.whiteColor
                cell?.backgroundColor = UIColor.darkGray

            }else{
                cell?.textLabel?.text = "No More Data Found"
                cell?.textLabel?.textColor = UIColor.darkGray
                cell?.backgroundColor = UIColor.gray
                cell?.selectionStyle = UITableViewCellSelectionStyle.none
            }
        case 3:
            if subCatagorizeDataTwoS.count != forS
            {
                cell?.textLabel?.text = "Load More Data"
                cell?.textLabel?.textColor = Color.whiteColor
                cell?.backgroundColor = UIColor.darkGray

            }else{
                cell?.textLabel?.text = "No More Data Found"
                cell?.textLabel?.textColor = UIColor.darkGray
                cell?.backgroundColor = UIColor.gray
                cell?.selectionStyle = UITableViewCellSelectionStyle.none
            }
        case 4:
            if subCatagorizeDataTwoW.count != forW
            {
                cell?.textLabel?.text = "Load More Data"
                cell?.textLabel?.textColor = Color.whiteColor
                cell?.backgroundColor = UIColor.darkGray

            }else{
                cell?.textLabel?.text = "No More Data Found"
                cell?.textLabel?.textColor = UIColor.darkGray
                cell?.backgroundColor = UIColor.gray
                cell?.selectionStyle = UITableViewCellSelectionStyle.none
            }

        default:break
        }

        return cell!
    }
    else
    {
        let tableObjects:StorageSubCatagoriez3rd = SubCatagoriez3rd[indexPath.row]
        let cell = self.bussinessTableView.dequeueReusableCell(withIdentifier: "bussinessCell3")
        cell?.textLabel?.text = tableObjects.nameString
        return cell!

    }
}

enter image description here

Kunal Parekh
  • 380
  • 6
  • 24
  • Check this answer: https://stackoverflow.com/a/44960181/8234523 – adev Aug 03 '17 at 04:01
  • what is tableObjects.nameString can you give source array and your expected output – Jaydeep Vyas Aug 10 '17 at 03:53
  • tableObjects.nameString is the name of the profile that is fetched from the database. – Kunal Parekh Aug 10 '17 at 04:04
  • ok, I have just updated my question. If you can see in the screenshot, that I have a business for example, George's Concreting, this business is repeated more than one time. Actually, it should only repeat once here. When I press on North, if this business is listed in this region, this business should then appear, or else hidden. – Kunal Parekh Aug 10 '17 at 04:07
  • @JaydeepVyas, tableObjects.nameString is holding the value of Profile Name that is fetched from the external database. And apparently, the value that is shown on each cell of the tableview, is duplicated. I need to remove the duplicates – Kunal Parekh Aug 10 '17 at 04:17
  • @JaydeepVyas, as you asked me what is the source array, it is StorageSubCatagoriezTwo – Kunal Parekh Aug 10 '17 at 04:18

2 Answers2

0

As per my knowledge if you want to remove duplicate from array then use below logic with your datatype

func removeDublicate (sourceArray: [Int]) -> [Int] {
    var uniqueArray:[Int] = []
    for object in sourceArray {
        if !uniqueArray.contains(object)
        {
            uniqueArray.append(object)
        }
    }
    return uniqueArray
}

let testing = removeDublicate(sourceArray: [1,2,2,5,1,8,3,1,2,8,5])
print(testing)

OUTPUT

[1, 2, 5, 8, 3]

Replace this line

let tableObjects:StorageSubCatagoriezTwoNorth = subCatagorizeDataTwoN[indexPath.row]

With this

  var tableObjects:StorageSubCatagoriezTwoNorth = subCatagorizeDataTwoN[indexPath.row]
    tableObjects = removeDuplicate(sourceArray: [tableObjects.nameString ])

Do it for all regions

Jaydeep Vyas
  • 4,411
  • 1
  • 18
  • 44
  • hi Jaydeep, I tried your suggestion but apparently it still does not work accurately. I hav updated my code above, please have a look. Thanks – Kunal Parekh Aug 09 '17 at 23:13
  • you have to remove duplicate from main array when your data is first time loaded – Jaydeep Vyas Aug 10 '17 at 04:20
  • above method is used to remove duplicates from array your tableObjects isn not array at seem is method ` let tableObjects:StorageSubCatagoriez3rd = SubCatagoriez3rd[indexPath.row]` – Jaydeep Vyas Aug 10 '17 at 04:23
  • ok. then how can i use this to point out to the array. cos the "subCatagorizeDataTwo" holds the array values – Kunal Parekh Aug 10 '17 at 04:26
  • I tried it with few other possibilities, but cant figure it out. Can you please help me? Thanks – Kunal Parekh Aug 10 '17 at 04:33
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/151558/discussion-between-sarah-malik-and-jaydeep-vyas). – Kunal Parekh Aug 10 '17 at 04:41
  • i tried using replacing the line but now having this error Cannot assign value of type '[String]' to type 'StorageSubCatagoriezTwoNorth' – Kunal Parekh Aug 10 '17 at 04:43
-1

In swift 3 you can use this code for removing duplicate from array. Only this much code is needed. Try this code..

import UIKit

class ViewController2: UIViewController {

   var myArray: [Int] = [1,2,3,4,3,2,5,7,5,8]

   override func viewDidLoad()
   {
       super.viewDidLoad()

       let uniqueArray = Array(Set(self.myArray))
       print(uniqueArray) // This print result will be [2, 4, 5, 7, 1, 8]
   }
}

OUTPUT

[2, 4, 5, 7, 1, 8]

Wide Angle Technology
  • 1,184
  • 1
  • 8
  • 28
  • 1
    your answer is only partially correct. The 3 wouldn't be removed only the duplicates of it. The final output would be [2, 4, 5, 7, 3, 1, 8] – Lance Samaria Aug 22 '17 at 18:24